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

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.

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).

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

Java: 190.5 seconds

C++: 189 seconds

**C#: 242 seconds**


To calculate the first 100000 primes (one run) it took:

Java: 591 seconds

C++: 588 seconds

**C#: 771 seconds**


**So my question, why is C# much slower than the other 2?** 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):

    static void Main(string[] args)
    {
    
        int NumberOfPrimesToFind = 100000;
        int NumberOfRuns = 1;
    
        DateTime start = DateTime.Now;
        for (int k = 0; k < 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 < NumberOfPrimesToFind)
        {
            IsPrime = true;
    
            for (int j = 2; j < CurrentPossible; j++)
            {
                if (CurrentPossible % j == 0)
                {
                    IsPrime = false;
                    break;
                }
            }
    
            if (IsPrime)
            {
                NumberOfPrimes++;
            }
    
            CurrentPossible++;
        }
    }
Why is C# much slower than Java and C++ in my prime number testing
===

(bear with me, there's a lot of explanation before my question)

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.

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).

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

Java: 190.5 seconds

C++: 189 seconds

**C#: 242 seconds**


To calculate the first 100000 primes (one run) it took:

Java: 591 seconds

C++: 588 seconds

**C#: 771 seconds**


**So my question, why is C# much slower than the other 2?** 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):

    static void Main(string[] args)
    {
    
        int NumberOfPrimesToFind = 100000;
        int NumberOfRuns = 1;
    
        DateTime start = DateTime.Now;
        for (int k = 0; k < 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 < NumberOfPrimesToFind)
        {
            IsPrime = true;
    
            for (int j = 2; j < CurrentPossible; j++)
            {
                if (CurrentPossible % j == 0)
                {
                    IsPrime = false;
                    break;
                }
            }
    
            if (IsPrime)
            {
                NumberOfPrimes++;
            }
    
            CurrentPossible++;
        }
    }