public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Brown <david@westcontrol.com>
To: gcc-help@gcc.gnu.org
Subject: Re: GCC Optimization Levels - Seeking Insights
Date: Fri, 22 Dec 2023 10:24:16 +0100	[thread overview]
Message-ID: <um3kk0$kf6$1@ciao.gmane.io> (raw)
In-Reply-To: <CAH6eHdRMaD09K3HKMOCr35XGXrjnoGtTJLR5GZ7LAO_fjP7-Yg@mail.gmail.com>

On 21/12/2023 20:57, Jonathan Wakely via Gcc-help wrote:
> On Thu, 21 Dec 2023, 19:33 Aran Nokan via Gcc-help, <gcc-help@gcc.gnu.org>
> wrote:
> 
>> Hello GCC Community,
>>
>> I recently conducted an experiment where I tested the impact of different
>> GCC optimization levels on the performance of a code
>> <https://github.com/leechwort/levenberg-maquardt-example>. I observed that
>> higher optimization levels didn't necessarily result in faster code
>> execution.
>>
>> Is it correct or I have made a mistake? Do we have any other parameters for
>> optimization?
>>
> 
> There is no guarantee that optimization make code faster, except that -O0
> will invariably be slower.
> 
> 
> However, I suspect the problem is that your makefile only builds the .o
> objects once, for the first target that needs them. That means they will be
> optimized (or not optimized) according to the CFLAGS set when building that
> first target.
> 
> Unless you do 'make clean' before building each target, you weren't
> actually testing what you intend to test.
> 
> 

For this test, since there are not many source files, I'd put them all 
on the same gcc line :


default: all
.PHONY: all


OPTS = 0 1 2 3 g fast

PROGS = $(foreach opt, $(OPTS), test.$(opt))
all: $(PROGS)

CC = gcc
CFLAGS_COMMON = -I.
LDLAGS = -lm
DEPS = levmarq.h makefile
SRC = main.c levmarg.c

test.% : $(SRC) $(DEPS)
	gcc -o $@ $(CFLAGS) $(LDLAGS) -O$* $(SRC)

clean:
	rm -f $(PROGS)



(I haven't tried the makefile at all, but it might be a starting point 
for the OP.)


>>
>> My make file was as follows:
>>
>>
>> CC=gcc
>>> CFLAGS_COMMON=-I.
>>> LDLAGS=-lm
>>> DEPS = levmarq.h
>>> OBJ = main.o levmarq.o
>>>
>>> %.o: %.c $(DEPS)
>>>          $(CC) -c -o $@ $< $(CFLAGS) $(LDLAGS)
>>>
>>> main: $(OBJ)
>>>          gcc -o $@ $^ $(CFLAGS) $(LDLAGS)
>>>
>>> # No optimization
>>> main_no_opt: CFLAGS += -O0
>>> main_no_opt: $(OBJ)
>>>          gcc -o $@ $^ $(CFLAGS) $(LDLAGS)
>>>
>>> # Basic optimization
>>> main_opt1: CFLAGS += -O1
>>> main_opt1: $(OBJ)
>>>          gcc -o $@ $^ $(CFLAGS) $(LDLAGS)
>>>
>>> # Moderate optimization
>>> main_opt2: CFLAGS += -O2
>>> main_opt2: $(OBJ)
>>>          gcc -o $@ $^ $(CFLAGS) $(LDLAGS)
>>>
>>> # High optimization
>>> main_opt3: CFLAGS += -O3
>>> main_opt3: $(OBJ)
>>>          gcc -o $@ $^ $(CFLAGS) $(LDLAGS)
>>>
>>> # Clean rule
>>> clean:
>>>          rm -f *.o main main_no_opt main_opt2 main_opt3
>>>
>>
>> Best regards,
>> Aran
>>
> 



  reply	other threads:[~2023-12-22  9:24 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-21 19:32 Aran Nokan
2023-12-21 19:57 ` Jonathan Wakely
2023-12-22  9:24   ` David Brown [this message]
2023-12-22 10:44     ` Aran Nokan
2023-12-22 11:25       ` David Brown
2023-12-22 11:51       ` Jonathan Wakely
2023-12-22 11:52         ` Jonathan Wakely
2023-12-25 16:21     ` NightStrike
2023-12-26 18:46       ` Segher Boessenkool
2023-12-24 13:54 ` Peter0x44

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='um3kk0$kf6$1@ciao.gmane.io' \
    --to=david@westcontrol.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).