tags:

views:

121

answers:

3

Can a static function in C be potentially faster because the compiler's optimizer sees all the call sites and therefore optimizes the epilog and prolog of the called function?

+2  A: 

It can make the compiler more willing to inline, yes. But, as always, it depends on the compiler. You have to test and check the output assembly to be sure.

kotlinski
+3  A: 

If your function is called from the same translation unit as where it's defined (which static functions are obviously required to be), compilers can already easily inline such calls, whether the function is declared static or not.

Some quality compilers will also perform whole-program optimisation, so that inlining and other optimisations can occur even for calls to functions in a different translation unit.

Chris Jester-Young
Yes! Can you say `LLVM`? What seem like absolutes in the practice of programming often turn out to be ephemera.
Tim Schaeffer
@Tim: +1 <3 LLVM (and other dynamic compilation systems). But seriously, even the higher-end editions of Visual C++ will do whole-program optimisations for you.
Chris Jester-Young
And poor GCC can't do it yet :( But i heard work is underway to add it.
Johannes Schaub - litb
+3  A: 

It in theory it can. Yet at the same time some modern compilers can perform so called "global optimizations", which are based on analyzing relationships between the code across translation units. This can include analyzing all the call sites for a given function in the entire program (as opposed to a single translation unit) and potentially extend such optimizations to non-static functions as well.

AndreyT