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?
- 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.)C. K. Young– C. K. Young2010-02-12 15:38:51 +00:00Commented Feb 12, 2010 at 15:38
- @Chris: I wish I knew that. Can I change it now?Mike– Mike2010-02-12 15:50:46 +00:00Commented 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/…C. K. Young– C. K. Young2010-02-12 15:54:09 +00:00Commented Feb 12, 2010 at 15:54
- Another link, on the "politics of CW", which I found fascinating: meta.stackexchange.com/questions/10390/…C. K. Young– C. K. Young2010-02-12 15:59:40 +00:00Commented Feb 12, 2010 at 15:59
3 Answers
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.
Comments
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
LLVM? What seem like absolutes in the practice of programming often turn out to be ephemera.