* GCC Optimization Levels - Seeking Insights
@ 2023-12-21 19:32 Aran Nokan
2023-12-21 19:57 ` Jonathan Wakely
2023-12-24 13:54 ` Peter0x44
0 siblings, 2 replies; 10+ messages in thread
From: Aran Nokan @ 2023-12-21 19:32 UTC (permalink / raw)
To: gcc-help
[-- Attachment #1: Type: text/plain, Size: 1208 bytes --]
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?
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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: GCC Optimization Levels - Seeking Insights 2023-12-21 19:32 GCC Optimization Levels - Seeking Insights Aran Nokan @ 2023-12-21 19:57 ` Jonathan Wakely 2023-12-22 9:24 ` David Brown 2023-12-24 13:54 ` Peter0x44 1 sibling, 1 reply; 10+ messages in thread From: Jonathan Wakely @ 2023-12-21 19:57 UTC (permalink / raw) To: Aran Nokan; +Cc: gcc-help [-- Attachment #1: Type: text/plain, Size: 1849 bytes --] 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. > > 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 > ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: GCC Optimization Levels - Seeking Insights 2023-12-21 19:57 ` Jonathan Wakely @ 2023-12-22 9:24 ` David Brown 2023-12-22 10:44 ` Aran Nokan 2023-12-25 16:21 ` NightStrike 0 siblings, 2 replies; 10+ messages in thread From: David Brown @ 2023-12-22 9:24 UTC (permalink / raw) To: gcc-help 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 >> > ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: GCC Optimization Levels - Seeking Insights 2023-12-22 9:24 ` David Brown @ 2023-12-22 10:44 ` Aran Nokan 2023-12-22 11:25 ` David Brown 2023-12-22 11:51 ` Jonathan Wakely 2023-12-25 16:21 ` NightStrike 1 sibling, 2 replies; 10+ messages in thread From: Aran Nokan @ 2023-12-22 10:44 UTC (permalink / raw) To: David Brown; +Cc: gcc-help [-- Attachment #1: Type: text/plain, Size: 3288 bytes --] Hi, Thank you. I noticed that it is not working with the following error: No rule to make target 'test.0', So I changed it to this: $(PROGS): test.% : $(SRC) $(DEPS) > $(CC) -o $@ $(CFLAGS) $(LDLAGS) -O$* $(SRC) But now another problem: make: *** No rule to make target 'levmarg.c', needed by 'test.0'. Stop. > Do you have any idea how to fix it? Sincerely, Aran On Fri, Dec 22, 2023 at 10:26 AM David Brown via Gcc-help < gcc-help@gcc.gnu.org> wrote: > 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 > >> > > > > > ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: GCC Optimization Levels - Seeking Insights 2023-12-22 10:44 ` Aran Nokan @ 2023-12-22 11:25 ` David Brown 2023-12-22 11:51 ` Jonathan Wakely 1 sibling, 0 replies; 10+ messages in thread From: David Brown @ 2023-12-22 11:25 UTC (permalink / raw) To: gcc-help On 22/12/2023 11:44, Aran Nokan via Gcc-help wrote: > Hi, > > Thank you. I noticed that it is not working with the following error: > > No rule to make target 'test.0', > > > So I changed it to this: > > $(PROGS): test.% : $(SRC) $(DEPS) >> $(CC) -o $@ $(CFLAGS) $(LDLAGS) -O$* $(SRC) > > > But now another problem: > > make: *** No rule to make target 'levmarg.c', needed by 'test.0'. Stop. >> > > Do you have any idea how to fix it? Yes, that one's easy - fix my spelling mistake! I think your file is called "levmarq.c", while I had written "levmarg.c" (with "g" instead of "q"). As I said, I had not tested the makefile at all - it was just a suggestion that might help you, and it avoids the problem Jonathan pointed out where you were not re-compiling the .o files with the right optimisation flags. If this is just for one-off experimentation, you could just use your original makefile but with a "make clean" before each "make main_opt?" run. David > > Sincerely, > Aran > > On Fri, Dec 22, 2023 at 10:26 AM David Brown via Gcc-help < > gcc-help@gcc.gnu.org> wrote: > >> 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 >>>> >>> >> >> >> > ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: GCC Optimization Levels - Seeking Insights 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 1 sibling, 1 reply; 10+ messages in thread From: Jonathan Wakely @ 2023-12-22 11:51 UTC (permalink / raw) To: Aran Nokan; +Cc: David Brown, gcc-help [-- Attachment #1: Type: text/plain, Size: 1475 bytes --] On Fri, 22 Dec 2023, 10:45 Aran Nokan via Gcc-help, <gcc-help@gcc.gnu.org> wrote: > Hi, > > Thank you. I noticed that it is not working with the following error: > > No rule to make target 'test.0', > > > So I changed it to this: > > $(PROGS): test.% : $(SRC) $(DEPS) > > $(CC) -o $@ $(CFLAGS) $(LDLAGS) -O$* $(SRC) > > > But now another problem: > > make: *** No rule to make target 'levmarg.c', needed by 'test.0'. Stop. > > > > Do you have any idea how to fix it? > You spelled it wrong. levmarq not levmarq (N.B. This is not a question about using gcc, this is just about how to write a working makefile) > > > >>> > > >>> 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 > > >> > > > > > > > > > > ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: GCC Optimization Levels - Seeking Insights 2023-12-22 11:51 ` Jonathan Wakely @ 2023-12-22 11:52 ` Jonathan Wakely 0 siblings, 0 replies; 10+ messages in thread From: Jonathan Wakely @ 2023-12-22 11:52 UTC (permalink / raw) To: Aran Nokan; +Cc: David Brown, gcc-help [-- Attachment #1: Type: text/plain, Size: 684 bytes --] On Fri, 22 Dec 2023, 11:51 Jonathan Wakely, <jwakely.gcc@gmail.com> wrote: > > > On Fri, 22 Dec 2023, 10:45 Aran Nokan via Gcc-help, <gcc-help@gcc.gnu.org> > wrote: > >> Hi, >> >> Thank you. I noticed that it is not working with the following error: >> >> No rule to make target 'test.0', >> >> >> So I changed it to this: >> >> $(PROGS): test.% : $(SRC) $(DEPS) >> > $(CC) -o $@ $(CFLAGS) $(LDLAGS) -O$* $(SRC) >> >> >> But now another problem: >> >> make: *** No rule to make target 'levmarg.c', needed by 'test.0'. Stop. >> > >> >> Do you have any idea how to fix it? >> > > You spelled it wrong. levmarq not levmarq > Ha, my phone's autocorrect changed the second one. ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: GCC Optimization Levels - Seeking Insights 2023-12-22 9:24 ` David Brown 2023-12-22 10:44 ` Aran Nokan @ 2023-12-25 16:21 ` NightStrike 2023-12-26 18:46 ` Segher Boessenkool 1 sibling, 1 reply; 10+ messages in thread From: NightStrike @ 2023-12-25 16:21 UTC (permalink / raw) To: David Brown; +Cc: gcc-help [-- Attachment #1: Type: text/plain, Size: 364 bytes --] On Fri, Dec 22, 2023, 04:25 David Brown via Gcc-help <gcc-help@gcc.gnu.org> wrote: > > 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 > It's worth testing with march=native and flto, both provide easy wins if you can accept any implied limitations. > ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: GCC Optimization Levels - Seeking Insights 2023-12-25 16:21 ` NightStrike @ 2023-12-26 18:46 ` Segher Boessenkool 0 siblings, 0 replies; 10+ messages in thread From: Segher Boessenkool @ 2023-12-26 18:46 UTC (permalink / raw) To: NightStrike; +Cc: David Brown, gcc-help On Mon, Dec 25, 2023 at 11:21:45AM -0500, NightStrike via Gcc-help wrote: > On Fri, Dec 22, 2023, 04:25 David Brown via Gcc-help <gcc-help@gcc.gnu.org> > wrote: > > OPTS = 0 1 2 3 g fast > > It's worth testing with march=native and flto, both provide easy wins if > you can accept any implied limitations. Also -Os is interesting. Segher ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Re: GCC Optimization Levels - Seeking Insights 2023-12-21 19:32 GCC Optimization Levels - Seeking Insights Aran Nokan 2023-12-21 19:57 ` Jonathan Wakely @ 2023-12-24 13:54 ` Peter0x44 1 sibling, 0 replies; 10+ messages in thread From: Peter0x44 @ 2023-12-24 13:54 UTC (permalink / raw) To: nokanaran, gcc-help Not related to the question directly, but I found the following talk on the different optimization settings of GCC to be very informative. https://youtu.be/GufwdypTfrE It really helped my understanding, and I also saw how big of a difference LTO made. Best wishes, Peter D. ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2023-12-26 18:47 UTC | newest] Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2023-12-21 19:32 GCC Optimization Levels - Seeking Insights Aran Nokan 2023-12-21 19:57 ` Jonathan Wakely 2023-12-22 9:24 ` David Brown 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
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).