Does anyone know if there is a list of what a compiler do to optimize a source code? I prefer GCC as example.
I want to know what a programmer should do with the code to get good optimization and help the compiler to optimize it. Some optimizations by programmer may avoid the compiler to do better optimizations.
Examples:
replace
for (int i = 0; i < n - 1; i++ )
by
int n2 = n - 1;
for (int i = 0; i < n2; i++ )
for (int i = 0; i < n/2; i++ )
by
int n2 = n/2;
for (int i = 0; i < n2; i++ )
for (int i = 0; i < obj.calc_value(); i++ ) //calc_value() will return the same result with obj remaining unchanged.
by
int y = obj.calc_value()
for (int i = 0; i < y; i++ )
It is important to keep the code simple to read and understand.
Thanks
Edit:
Other examples:
- Inline functions
- Remove recursion
Seriously, just leave that up to the compiler. I’ve seen the code that gcc outputs at its “insane”
-O3level and it’s proof positive that the people who wrote those optimisation engines are either aliens or from a substantially distant future time.I’ve yet to see a situation where
registerorinlinemade an appreciable difference in performance of my code. That doesn’t mean it won’t, just that the compiler writers know far more tricks than us mere mortals when it comes to extracting the last ounce of performance from the processor.As far as optimisation goes, it should only be done where there is a real problem. That means profiling code and discovering bottlenecks but, more importantly, not optimising an operation that is not deemed slow in context. There zero difference to a user whether a one-shot operation takes a tenth of a second or a hundredth.
And sometimes, optimisation for readability is the best one you can do 🙂
As an aside, this is just one of the nifty tricks gcc does for you. Consider the following code which is supposed to calculate the factorial and return it:
This compiles to (at
-O3):That’s right, gcc just works it all out at compile-time and turns the whole thing into the equivalent of:
Contrast this with the
-O0(naive) version: