Change I solved the problem. The reason was a mistake in the testing procedure and will be described in detail as soon as I am allowed to answer my own question.
I know that this type of question should generally be avoided, but I came across a really strange situation that I cannot understand about. I tried to implement PRNG, and I tested its performance compared to System.Random. I found that my code was ~ 50 times slower, but it was not the algorithm that was the problem, but just a method call. Even if I had just returned a constant, it would still be many times slower.
So, I am writing a simple test program that compares a method call that wraps random.NextDouble (), a method that returns -1, and calls random.NextDouble () directly. I conducted a test at Ideone, and gave the expected results ; all the time were similar, and the return of the constant was the fastest. The time was about 0.1 seconds.
However, the same code compiled in Visual Studio 2011 Beta or C # Express 2010 will result in 4 seconds, 4 seconds, and 0.1 seconds for each case, respectively. I definitely work in release mode, the "Optimize code" checkbox is checked, and starting from outside Visual Studio gives the same results. So why are such simple method calls much slower in Visual Studio than Ideone? Here is the code I used for comparison:
using System; using System.Diagnostics; public class Test{ static Random random = new Random(); public static Double Random() { return random.NextDouble(); } public static Double Random2() { return -1; } public static void Main() { { Stopwatch s = new Stopwatch(); Double a = 0; s.Start(); for (Int32 i = 0; i < 5000000; i++) a += Random(); s.Stop(); Console.WriteLine(s.ElapsedMilliseconds); } { Stopwatch s = new Stopwatch(); Double a = 0; s.Start(); for (Int32 i = 0; i < 5000000; i++) a += Random2(); s.Stop(); Console.WriteLine(s.ElapsedMilliseconds); } { Stopwatch s = new Stopwatch(); Double a = 0; s.Start(); for (Int32 i = 0; i < 5000000; i++) a += random.NextDouble(); s.Stop(); Console.WriteLine(s.ElapsedMilliseconds); } } }
performance c # prng
Kira chow
source share