Is access to a static function variable slower than access to a global variable?

后端 未结 4 1940
慢半拍i
慢半拍i 2021-02-05 04:21

Static local variables are initialised on the first function call:

Variables declared at block scope with the specifier static have static storage duratio

4条回答
  •  广开言路
    2021-02-05 05:06

    From https://en.cppreference.com/w/cpp/language/initialization

    Deferred dynamic initialization
    It is implementation-defined whether dynamic initialization happens-before the first statement of the main function (for statics) or the initial function of the thread (for thread-locals), or deferred to happen after.

    If the initialization of a non-inline variable (since C++17) is deferred to happen after the first statement of main/thread function, it happens before the first odr-use of any variable with static/thread storage duration defined in the same translation unit as the variable to be initialized.

    So similar check may have to be done also for global variables.

    so f() is not necessary "slower" than g().

提交回复
热议问题