title: Why is fastcall slower than stdcall?
body: <p>I found following question: <a href="https://stackoverflow.com/questions/2188680/is-fastcall-really-faster">Is fastcall really faster?</a></p>
  
  <p>No clear answers for x86 were given so I decided to create benchmark.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;time.h&gt;
  
  int __fastcall func(int i)
  {   
      return i + 5;
  }
  
  int _stdcall func2(int i)
  {   
      return i + 5;
  }
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      int iter = 100;
      int x = 0;
      clock_t t = clock();
      for (int j = 0; j &lt;= iter;j++)
          for (int i = 0; i &lt;= 1000000;i++)
              x = func(x &amp; 0xFF);
      printf("%d\n", clock() - t);
      t = clock();
      for (int j = 0; j &lt;= iter;j++)
          for (int i = 0; i &lt;= 1000000;i++)
              x = func2(x &amp; 0xFF);
      printf("%d\n", clock() - t);
      printf("%d", x);
      return 0;
  }
  </code></pre>
  
  <p>In case of no optimization result in MSVC 10 is:</p>
  
  <pre><code>4671
  4414
  </code></pre>
  
  <p>With max optimization <code>fastcall</code> is sometimes faster, but I guess it is multitasking noise. Here is average result (with <code>iter = 5000</code>)</p>
  
  <pre><code>6638
  6487
  </code></pre>
  
  <p><code>stdcall</code> looks faster!</p>
  
  <p>Here are results for GCC: <a href="http://ideone.com/hHcfP" rel="noreferrer">http://ideone.com/hHcfP</a>
  Again, <code>fastcall</code> lost race.</p>
  
  <p>Here is part of disassembly in case of <code>fastcall</code>:</p>
  
  <pre><code>011917EF  pop         ecx  
  011917F0  mov         dword ptr [ebp-8],ecx  
      return i + 5;
  011917F3  mov         eax,dword ptr [i]  
  011917F6  add         eax,5
  </code></pre>
  
  <p>this is for <code>stdcall</code>:</p>
  
  <pre><code>    return i + 5;
  0119184E  mov         eax,dword ptr [i]  
  01191851  add         eax,5  
  </code></pre>
  
  <p><code>i</code> is passed via <code>ECX</code>, instead of stack, but saved into stack in the body! So all the effect is neglected! this simple function can be calculated using only registers! And there is no real difference between them.</p>
  
  <p>Can anyone explain what is reason for <code>fastcall</code>? Why doesn't it give speedup?</p>
  
  <p><strong>Edit:</strong> With optimization it turned out that both functions are inlined. When I turned inlining off they both are compiled to:</p>
  
  <pre><code>00B71000  add         eax,5  
  00B71003  ret  
  </code></pre>
  
  <p>This looks like great optimization, indeed, but it doesn't respect calling conventions at all, so test is not fair.</p>