Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple method call is really slow?
    primarykey
    data
    text
    <p><strong>Edit</strong>: I've resolved my problem. The cause was an error in testing procedure and will be detailed once I'm allowed to answer my own question. </p> <p>I know this type of question should generally be avoided, but I've come across a really strange situation that I can't make sense of. I've been trying to implement a PRNG, and I've been testing its performance against System.Random. I found that my code was ~50 times slower, but it wasn't the algorithm that was the problem, but just calling the method. Even if I just returned a constant, it would still be many times slower. </p> <p>So I write a simple test program that compares calling a method that wraps random.NextDouble(), a method that returns -1, and calling random.NextDouble() directly. I ran my test in Ideone, and <a href="http://ideone.com/ifJLE" rel="nofollow">it gave the expected results</a>; all the times were similar, and returning a constant was fastest. The times were all around 0.1 seconds. </p> <p>However, the same code compiled in Visual Studio 2011 Beta or 2010 C# Express would result in 4 seconds, 4 seconds, and 0.1 seconds, for each case respectively. I'm definitely running in release mode, the optimize code checkbox is ticked, and launching from outside Visual Studio gives the same results. So why are such simple method calls so much slower in Visual Studio than Ideone? Here's the code I used to benchmark:</p> <pre><code>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 &lt; 5000000; i++) a += Random(); s.Stop(); Console.WriteLine(s.ElapsedMilliseconds); } { Stopwatch s = new Stopwatch(); Double a = 0; s.Start(); for (Int32 i = 0; i &lt; 5000000; i++) a += Random2(); s.Stop(); Console.WriteLine(s.ElapsedMilliseconds); } { Stopwatch s = new Stopwatch(); Double a = 0; s.Start(); for (Int32 i = 0; i &lt; 5000000; i++) a += random.NextDouble(); s.Stop(); Console.WriteLine(s.ElapsedMilliseconds); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload