GCC Optimization Levels - Seeking Insights

Jonathan Wakely jwakely.gcc@gmail.com
Thu Dec 21 19:57:36 GMT 2023


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
>


More information about the Gcc-help mailing list