GCC optimization is a good document but a little verbose. I summarize the takeaways of it in this post:
(1) Use -march
to generate the optimized code for specified CPU. E.g., -march=native
. But beware that the code will not have backwards compatibility for older/different CPUs.
(2) -O2
is recommended. But if play with cmake
, “cmake -DCMAKE_BUILD_TYPE=Release ..
” will use -O3
to compile code. At least from my own experience,-O3
should be OK.
(3) -pipe
has no effect on the generated code but makes compilation fast. If the system has enough memory, use it.
So the best method of compiling code using gcc
should be:
# gcc -march=native -O2 -pipe test.c -o test
Or use -O3
instead of -O2
? I dunno, choose -O2
or -O3
at your own risk.