title: Why does C# execute Math.Sqrt() more slowly than VB.NET?
body: <h3>Background</h3>
  <p>While running benchmark tests this morning, my colleagues and I discovered some strange things concerning performance of C# code vs. VB.NET code.</p>
  <p>We started out comparing C# vs. Delphi Prism calculating prime numbers, and found that Prism was about 30% faster. I figured CodeGear optimized code more when generating IL (the <code>exe</code> was about twice as big as C#'s and had all sorts of different IL in it.)</p>
  <p>I decided to write a test in VB.NET as well, assuming that Microsoft's compilers would end up writing essentially the same IL for each language. However, the result there was more shocking: <strong>the code ran more than three times slower on C# than VB with the same operation!</strong></p>
  <p>The generated IL was different, but not extremely so, and I'm not good enough at reading it to understand the differences.</p>
  <h3>Benchmarks</h3>
  <p>I've included the code for each below. On my machine, VB finds 348513 primes in about <strong>6.36</strong> seconds. C# finds the same number of primes in <strong>21.76</strong> seconds.</p>
  <h3>Computer Specs and Notes</h3>
  <ul>
  <li>Intel Core 2 Quad 6600 @ 2.4Ghz</li>
  </ul>
  <p>Every machine I've tested on there is a noticeable difference in the benchmark results between C# and VB.NET.</p>
  <p>Both of the console applications were compiled in Release mode, but otherwise no project settings were changed from the defaults generated by Visual Studio 2008.</p>
  <h3>VB.NET code</h3>
  <pre class="lang-vb.net prettyprint-override"><code>Imports System.Diagnostics
  
  Module Module1
  
      Private temp As List(Of Int32)
      Private sw As Stopwatch
      Private totalSeconds As Double
  
      Sub Main()
          serialCalc()
      End Sub
  
      Private Sub serialCalc()
          temp = New List(Of Int32)()
          sw = Stopwatch.StartNew()
          For i As Int32 = 2 To 5000000
              testIfPrimeSerial(i)
          Next
          sw.Stop()
          totalSeconds = sw.Elapsed.TotalSeconds
          Console.WriteLine(String.Format(&quot;{0} seconds elapsed.&quot;, totalSeconds))
          Console.WriteLine(String.Format(&quot;{0} primes found.&quot;, temp.Count))
          Console.ReadKey()
      End Sub
  
      Private Sub testIfPrimeSerial(ByVal suspectPrime As Int32)
          For i As Int32 = 2 To Math.Sqrt(suspectPrime)
              If (suspectPrime Mod i = 0) Then
                  Exit Sub
              End If
          Next
          temp.Add(suspectPrime)
      End Sub
  
  End Module
  </code></pre>
  <h3>C# Code</h3>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Diagnostics;
  
  namespace FindPrimesCSharp {
      class Program {
          List&lt;Int32&gt; temp = new List&lt;Int32&gt;();
          Stopwatch sw;
          double totalSeconds;
  
  
          static void Main(string[] args) {
  
              new Program().serialCalc();
  
          }
  
  
          private void serialCalc() {
              temp = new List&lt;Int32&gt;();
              sw = Stopwatch.StartNew();
              for (Int32 i = 2; i &lt;= 5000000; i++) {
                  testIfPrimeSerial(i);
              }
              sw.Stop();
              totalSeconds = sw.Elapsed.TotalSeconds;
              Console.WriteLine(string.Format(&quot;{0} seconds elapsed.&quot;, totalSeconds));
              Console.WriteLine(string.Format(&quot;{0} primes found.&quot;, temp.Count));
              Console.ReadKey();
          }
  
          private void testIfPrimeSerial(Int32 suspectPrime) {
              for (Int32 i = 2; i &lt;= Math.Sqrt(suspectPrime); i++) {
                  if (suspectPrime % i == 0)
                      return;
              }
              temp.Add(suspectPrime);
          }
  
      }
  }
  </code></pre>
  <p>Why is C#'s execution of <code>Math.Sqrt()</code> slower than VB.NET?</p>