Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple method call is really slow?

Edit: 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.

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.

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 it gave the expected results; all the times were similar, and returning a constant was fastest. The times were all around 0.1 seconds.

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:

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);
        }
    }
}
like image 868
Kira Chow Avatar asked Apr 08 '12 19:04

Kira Chow


People also ask

Why is function call slow?

C, just like any language, is just as slow (or quick) as the assembly that one specific compiler generates. Since function-calls are incredibly platform-specific, you can't tie the cost of a function call to a language. The optimizations of function calls, however, can be tied to specific compilers.

Is calling functions in Python slow?

The short version is that it takes about 150ns to call a function in Python (on my laptop). This doesn't sound like a lot, but it means that you can make at most 6.7 million calls per second, two to three orders of magnitude slower than your processor's clock speed.

Do function calls slow down code?

Yes method calls slow down the code execution a tiny little bit, if they a not inlined by the c#-compiler or the jit-compiler. However, unless your code runs in a loop and is executed a million times or so, you should really focus on producing clean, understandable and maintainable code.

Are functions slower in Python?

A function never makes those statements themselves slower. A Python function is just an object stored in a variable; you can assign functions to a different variable, replace them with something completely different, or delete them at any time.


1 Answers

You shouldn't measure the first call to Random() and Random2(). The first time a function is called, it is handled by the JITTER. Instead, call Random() and Random2() once, then start measuring. random.NextDouble() was already compiled after .NET was installed, so it doesn't suffer from the same problem.

I don't believe this will explain all the difference, but it should level the playing field.

like image 190
zmbq Avatar answered Sep 23 '22 19:09

zmbq