Honestly, I seldom used “-g -O2
” option before when employ gcc
to compile projects since optimization will cause source code and instructions can’t be consistent, and it makes you annoyed during debugging. But in the past half a year, since my work involves a lot of encryption & decryption jobs, and they are all compute intensive tasks, I find the -O2
option can really give a big improvement in performance.
For example, when the project is compiled with only -g
option, the whole computation flow will last more than 30
minutes, but after using “-g -O2
” option, the time is reduced to less than 3
minutes, and the whole performance is 10x
times improved than before.
So when you care about your program’s performance, you should try to use “-g -O2
” option: the -g
option can enlarge your executable file size, but won’t make it run slow, and once the program crashes, it can also provide you enough debug information; the -O2
is the “best safe optimization” option. Besides these, you may run into some tricky bugs which occur only during optimization code.
Hope you can try and enjoy it!
References:
Using -g and -O2 options in gcc;
Is a program compiled with -g gcc flag slower than the same program compiled without -g?.