c++optimizationcompiler-optimization

Hints for the compiler to help it with the optimization task


The const and volatile chapter on the 'Surviving the Release Version' Article gave me the idea that the compiler can use the const keyword as hint for its optimization job.

Do you know some other optimization-hints for the compiler or design principles for functions so that the compiler can make them inline?

By the way, do you declare primitive-type function parameters as const or const reference (like void foo(const int i) or void foo(const int& i))?


Solution

  • It is rare that const qualification can help the compiler to optimize your code. You can read more about why this is the case in Herb Sutter's "Constant Optimization?"

    Concerning your last question: in general, you should prefer to pass by value things that are cheap to copy (like fundamental type objects--ints and floats and such--and small class type objects) and pass other types by const reference. This is a very general rule and there are lots of caveats and exceptions.