Why is C# much slower than Java and C++ in my prime number testing
<p>(bear with me, there's a lot of explanation before my question)</p>

<p>As a few of you may be aware over the last few days I have posted a fair few questions relating to performance of C++. Being a Java programmer I wanted to know was C++ worth the extra effort to specialise in, or is Java good enough for performance programming.</p>

<p>Anyway, I decided to write a basic (non-efficient algorithm) prime numbers program- no object creation, and to see how the 3 language compared time-wise. I wont post the code up, only for the reason that the code was exactly the same in the algorithm (except I used int instead of boolean in C++). Only the code for timing differed (and this was obviously outside the algorithm).</p>

<p>In summary, to calculate the first 10000 prime numbers and looped this 40 times (to exaggerate differences in speed) it took:</p>

<p>Java: 190.5 seconds</p>

<p>C++: 189 seconds</p>

<p><strong>C#: 242 seconds</strong></p>

<p>To calculate the first 100000 primes (one run) it took:</p>

<p>Java: 591 seconds</p>

<p>C++: 588 seconds</p>

<p><strong>C#: 771 seconds</strong></p>

<p><strong>So my question, why is C# much slower than the other 2?</strong> I thought Java and C# would be very close timings. Here is the C# algorithm (I am aware there are quicker ways of finding prime numbers):</p>

<pre><code>static void Main(string[] args)
{

    int NumberOfPrimesToFind = 100000;
    int NumberOfRuns = 1;

    DateTime start = DateTime.Now;
    for (int k = 0; k &lt; NumberOfRuns; k++)
    {
        FindPrimes(NumberOfPrimesToFind);

    }
    DateTime finish = DateTime.Now;
    Console.Out.WriteLine(finish-start);
    Console.In.Read();
}


static void FindPrimes(int NumberOfPrimesToFind)
{
    int NumberOfPrimes = 0;
    int CurrentPossible = 2;
    Boolean IsPrime ;

    while (NumberOfPrimes &lt; NumberOfPrimesToFind)
    {
        IsPrime = true;

        for (int j = 2; j &lt; CurrentPossible; j++)
        {
            if (CurrentPossible % j == 0)
            {
                IsPrime = false;
                break;
            }
        }

        if (IsPrime)
        {
            NumberOfPrimes++;
        }

        CurrentPossible++;
    }
}
</code></pre>

<p>C++</p>

<pre><code>int main()
{
    int NumberOfPrimesToFind = 100000;
    int NumberOfRuns = 1;
    int temp;
    double dif;

    time_t start,end;
    time (&amp;start);

    for (int k = 0; k &lt; NumberOfRuns; k++)
    {
        FindPrimes(NumberOfPrimesToFind);

    }

    time (&amp;end);

    //Number of seconds
    dif = difftime (end,start);

    cout &lt;&lt; (dif);
    cin &gt;&gt; temp;
}

void FindPrimes(int NumberOfPrimesToFind)
{
    int NumberOfPrimes = 0;
    int CurrentPossible = 2;
    int IsPrime ;

    while (NumberOfPrimes &lt; NumberOfPrimesToFind)
    {
        IsPrime = 1;

        for (int j = 2; j &lt; CurrentPossible; j++)
        {
            if (CurrentPossible % j == 0)
            {
                IsPrime = 0;
                break;
            }
        }

        if (IsPrime==1)
        {
            NumberOfPrimes++;
        }

        CurrentPossible++;
    }
}
</code></pre>

<p>Java:</p>

<pre><code>public static void main(String[] args) {
    int NumberOfPrimesToFind = 100000;
    int NumberOfRuns = 1;

    long start = System.currentTimeMillis();
    for (int k = 0; k &lt; NumberOfRuns; k++)
    {
        FindPrimes(NumberOfPrimesToFind);

    }
    long finish = System.currentTimeMillis();
    System.out.println((finish-start));

}

static void FindPrimes(int NumberOfPrimesToFind)
{
    int NumberOfPrimes = 0;
    int CurrentPossible = 2;
    Boolean IsPrime ;

    while (NumberOfPrimes &lt; NumberOfPrimesToFind)
    {
        IsPrime = true;

        for (int j = 2; j &lt; CurrentPossible; j++)
        {
            if (CurrentPossible % j == 0)
            {
                IsPrime = false;
                break;
            }
        }

        if (IsPrime)
        {
            NumberOfPrimes++;
        }

        CurrentPossible++;
    }
}
</code></pre>
