title: Is access to a static function variable slower than access to a global variable?
body: <p><a href="https://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables" rel="noreferrer">Static local variables</a> are initialised on the first function call:</p>
  
  <blockquote>
    <p>Variables declared at block scope with the specifier static have static storage duration but are initialized the first time control passes through their declaration (unless their initialization is zero- or constant-initialization, which can be performed before the block is first entered). On all further calls, the declaration is skipped.</p>
  </blockquote>
  
  <p>Also, in C++11 there are even more checks:</p>
  
  <blockquote>
    <p>If multiple threads attempt to initialize the same static local variable concurrently, the initialization occurs exactly once (similar behavior can be obtained for arbitrary functions with std::call_once).
    Note: usual implementations of this feature use variants of the double-checked locking pattern, which reduces runtime overhead for already-initialized local statics to a single non-atomic boolean comparison.   (since C++11)</p>
  </blockquote>
  
  <p>At the same time, <a href="https://en.cppreference.com/w/cpp/language/storage_duration#Storage_duration" rel="noreferrer">global variables seem to be initialised on program start</a> (though technically only <em>allocation</em>/<em>deallocation</em> is mentioned on cppreference):</p>
  
  <blockquote>
    <p>static storage duration. The storage for the object is allocated when the program begins and deallocated when the program ends. Only one instance of the object exists. All objects declared at namespace scope (including global namespace) have this storage duration, plus those declared with static or extern.</p>
  </blockquote>
  
  <p>So given the following example:</p>
  
  <pre><code>struct A {
      // complex type...
  };
  const A&amp; f()
  {
      static A local{};
      return local;
  }
  
  A global{};
  const A&amp; g()
  {
      return global;
  }
  </code></pre>
  
  <p>am I correct to assume that <code>f()</code> has to check whether its variable was initialised every time it is called and thus <code>f()</code> will be slower than <code>g()</code>?</p>