title: Try-catch speeding up my code?
body: <p>I wrote some code for testing the impact of try-catch, but seeing some surprising results.</p>
  
  <pre><code>static void Main(string[] args)
  {
      Thread.CurrentThread.Priority = ThreadPriority.Highest;
      Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
  
      long start = 0, stop = 0, elapsed = 0;
      double avg = 0.0;
  
      long temp = Fibo(1);
  
      for (int i = 1; i &lt; 100000000; i++)
      {
          start = Stopwatch.GetTimestamp();
          temp = Fibo(100);
          stop = Stopwatch.GetTimestamp();
  
          elapsed = stop - start;
          avg = avg + ((double)elapsed - avg) / i;
      }
  
      Console.WriteLine("Elapsed: " + avg);
      Console.ReadKey();
  }
  
  static long Fibo(int n)
  {
      long n1 = 0, n2 = 1, fibo = 0;
      n++;
  
      for (int i = 1; i &lt; n; i++)
      {
          n1 = n2;
          n2 = fibo;
          fibo = n1 + n2;
      }
  
      return fibo;
  }
  </code></pre>
  
  <p>On my computer, this consistently prints out a value around 0.96..</p>
  
  <p>When I wrap the for loop inside Fibo() with a try-catch block like this:</p>
  
  <pre><code>static long Fibo(int n)
  {
      long n1 = 0, n2 = 1, fibo = 0;
      n++;
  
      try
      {
          for (int i = 1; i &lt; n; i++)
          {
              n1 = n2;
              n2 = fibo;
              fibo = n1 + n2;
          }
      }
      catch {}
  
      return fibo;
  }
  </code></pre>
  
  <p>Now it consistently prints out 0.69... -- it actually runs faster! But why?</p>
  
  <p>Note: I compiled this using the Release configuration and directly ran the EXE file (outside Visual Studio).</p>
  
  <p>EDIT: <a href="https://stackoverflow.com/a/8928476/282110">Jon Skeet's <em>excellent</em> analysis</a> shows that try-catch is somehow causing the x86 CLR to use the CPU registers in a more favorable way in this specific case (and I think we're yet to understand why). I confirmed Jon's finding that x64 CLR doesn't have this difference, and that it was faster than the x86 CLR. I also tested using <code>int</code> types inside the Fibo method instead of <code>long</code> types, and then the x86 CLR was as equally fast as the x64 CLR.</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong> It looks like this issue has been fixed by Roslyn. Same machine, same CLR version -- the issue remains as above when compiled with VS 2013, but the problem goes away when compiled with VS 2015. </p>