About C #:
First of all, instead of datetime you should use a stopwatch. Datetime is not reliable for code synchronization.
Secondly, are you sure that you are performing it in release mode with a closed visual studio? If the visual studio is open or you start with F5, JIT will not optimize the code!
So ... use a stopwatch and close all instances of the visual studio. You have to change the project parameters, you have to have a combobox, on the top toolbar where you can read โDebugโ, just click on it and select โReleaseโ or go to the right click on your project, properties and change it to release. Then, to avoid any problems, close all instances of the visual studio and double-click on the executable file.
See http://msdn.microsoft.com/en-us/library/wx0123s5.aspx
CTRL + F5 does not compile in release mode, it simply launches the executable file in the selected compilation mode without attracting the debugging process, so if it was compiled in debugging, it will run the executable compiled in debugging mode without debugging.
Then I would suggest that you avoid using a boolean variable, each branch condition can slow down the processor, you can do this using an integer. This is valid for all languages, not just C #.
static void Main() { const int NumberOfPrimesToFind = 100000; const int NumberOfRuns = 1; System.Diagnostic.Stopwatch sw = new System.Diagnostic.Stopwatch(); sw.Start(); for (int k = 0; k < NumberOfRuns; k++) { FindPrimes(NumberOfPrimesToFind); } sw.Stop(); Console.WriteLine(sw.Elapsed.TotalMilliseconds); Console.ReadLine(); } static void FindPrimes(int NumberOfPrimesToFind) { int NumberOfPrimes = 0; int CurrentPossible = 2; while (NumberOfPrimes < NumberOfPrimesToFind) { int IsPrime = 1; for (int j = 2; j < CurrentPossible; j++) { if (CurrentPossible % j == 0) { IsPrime = 0; break; } } NumberOfPrimes += IsPrime; CurrentPossible++; } }
When you compile it with C ++ in release mode, since the input parameters are constants, the C ++ compiler is smart enough to perform some calculations at compile time (the power of modern C ++ compilers!). This magic is commonly used with templates, for example, STL (standard template library) is very slow in debug mode, but very fast in release mode.
In this case, the compiler completely excludes your function, because the output of your function is not used. Try to return an integer, the number of primes found and print it.
int FindPrimes(int NumberOfPrimesToFind) { int NumberOfPrimes = 0; int CurrentPossible = 2; while (NumberOfPrimes < NumberOfPrimesToFind) { int IsPrime = 1; for (int j = 2; j < CurrentPossible; j++) { if (CurrentPossible % j == 0) { IsPrime = 0; break; } } NumberOfPrimes += IsPrime; CurrentPossible++; } return NumberOfPrimes ; }
If you are curious about this aspect of the C ++ compiler, look, for example, at metaprogramming a template, there is formal evidence that the C ++ compiler is complete. As the Wikipedia quote quotes, โIn addition, templates are a C ++ compile-time mechanism that completes Turing, which means that any computation expressed by a computer program can be computed in some form by a template metaphone before runtime.โ http://en.wikipedia.org/wiki/C%2B%2B
However, I really hope that you use this algorithm only to understand how three different compilers / systems behave, because, of course, this is the worst algorithm that you can use to search for prime numbers, as indicated in other answers :)