title: Why does GCC generate a faster program than Clang in this recursive Fibonacci code?
body: <p>This is the code that I tested:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;chrono&gt;
  using namespace std;
  
  #define CHRONO_NOW                  chrono::high_resolution_clock::now()
  #define CHRONO_DURATION(first,last) chrono::duration_cast&lt;chrono::duration&lt;double&gt;&gt;(last-first).count()
  
  int fib(int n) {
      if (n&lt;2) return n;
      return fib(n-1) + fib(n-2);
  }
  
  int main() {
      auto t0 = CHRONO_NOW;
      cout &lt;&lt; fib(45) &lt;&lt; endl;
      cout &lt;&lt; CHRONO_DURATION(t0, CHRONO_NOW) &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <p>Of course, there are much faster ways of calculating Fibonacci numbers, but this is a good little stress test that focuses on recursive function calls.
  There's nothing else to the code, other than the use of chrono for measuring time.</p>
  
  <p>First I ran the test a couple of times in Xcode on OS X (so that's clang), using <code>-O3</code> optimization. It took about 9 seconds to run.</p>
  
  <p>Then, I compiled the same code with gcc (g++) on Ubuntu (using -O3 again), and that version only took about 6.3 seconds to run! Also, I was running Ubuntu <strong>inside VirtualBox</strong> on my mac, which could only affect the performance negatively, if at all.</p>
  
  <p>So there you go:</p>
  
  <ul>
  <li>Clang on OS X -> ~9 secs</li>
  <li>gcc on Ubuntu <strong>in VirtualBox</strong> -> ~6.3 secs.</li>
  </ul>
  
  <p>I know that these are completely different compilers so they do stuff differently, but all the tests I've seen featuring gcc and clang only showed much less of a difference, and in some cases, the difference was the other way around (clang being faster).</p>
  
  <p>So is there any logical explanation why gcc beats clang by miles in this particular example?</p>