6

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?

4
  • This question is actually a legit point-scoring question, and making it community-wiki will discourage legit answers. :-) (i.e., I think people don't prefer to answer "real" questions if it won't help them gain rep.) Commented Feb 12, 2010 at 15:38
  • @Chris: I wish I knew that. Can I change it now? Commented Feb 12, 2010 at 15:50
  • Nope, once a post becomes CW there is no way to un-CW it, by design. See: meta.stackexchange.com/questions/11740/… Commented Feb 12, 2010 at 15:54
  • Another link, on the "politics of CW", which I found fascinating: meta.stackexchange.com/questions/10390/… Commented Feb 12, 2010 at 15:59

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

3 Comments

Yes! Can you say LLVM? What seem like absolutes in the practice of programming often turn out to be ephemera.
@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.
And poor GCC can't do it yet :( But i heard work is underway to add it.
2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.