开发者

Can a static function be faster than a non-static one?

Can a static function in C be potentially faster because th开发者_StackOverflow社区e compiler's optimizer sees all the call sites and therefore optimizes the epilog and prolog of the called function?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜