简体   繁体   English

static关键字是否可以提高性能?

[英]Does the static keyword give any performance increase?

I appreciate that this is a micro-optimization, but I am interested in whether declaring either a function or member variable as static provides any performance increase compared to a non- static implementation? 我很欣赏这是一个微优化,但是我有兴趣将函数或成员变量声明为static与非static实现相比是否能提高性能?

I remember reading that const can be used for compilers to optimize, so it made me wonder whether static had any similar advantages. 我记得曾经读过const可用于编译器进行优化,所以让我想知道static是否具有任何类似的优点。

Considering that static and non- static variables have extremely different semantics, whether or not you can declare static really has nothing to do with performance. 考虑到static和非static变量具有非常不同的语义,你是否能申报static真的有无关的性能。

Also, cache and other issues might well mean "no". 另外,缓存和其他问题很可能意味着“否”。

it could be for data, if construction takes a long time (eg a precomputed buffer or something read from disk). 如果构造需要很长时间(例如,预先计算的缓冲区或从磁盘读取的内容),则可能用于数据。 often, this is only ideal when the data is immutable. 通常,这仅在数据不可变时才是理想的。

@DeadMG already pointed out that changing a member variable to static would completely change the semantics, the same applies to a static function. @DeadMG已经指出,将成员变量更改为静态会完全改变语义,静态函数也是如此。

In general the answer is no, static doesn't help performance ( and neither does const ) but one way that static can help is in an ELF shared library, where a static function isn't externally visible and so calls to it don't need to go through the procedure linkage table , which gives a small performance benefit which can be worth considering when writing code for shared libraries. 通常,答案是“否”, static对性能没有帮助( const也无济于事),但是static可以提供帮助的一种方法是在ELF共享库中,在该库中,静态函数在外部不可见,因此对其进行调用不会需要检查过程链接表 ,这会带来很小的性能优势,在为共享库编写代码时值得考虑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM