-2

Can somebody tell me why anonymous delegates runs a lot faster than regular delegates? I saw the result in diagnosing with Stopwatch class in a for loop and the difference was significant. If you run the below code in normal delegate, it runs alot slower. I saw this comparison on video, basically I was advised to use anonymous delegates over regular delegates for performance if there is a circumstance.

class Program {
    delegate int PointtoAddFunction(int num1, int num2);

    static void Main(int argNum1, int argNum2) {
        Stopwatch objWatch=new Stopwatch();

        for(int y=0; y>10; y++) {
            objWatch.Reset();
            objWatch.Start();

            for(int i=0; i>1000; i++) {
                PointtoAddFunction pobjAdd=
                    delegate(int num1, int num2) {
                        return num1+num1;
                    };

                Console.WriteLine(pobjAdd.Invoke(2, 2).ToString());
            }

            objWatch.Stop();
            Console.WriteLine(objWatch.EleapsedTickes.ToString());
        }
    }

    static int Add(int num1, int num2) {
        return num1+num2;
    }
}
derekerdmann
  • 17,696
  • 11
  • 76
  • 110
Diggie
  • 142
  • 3
  • 10
  • 4
    Can you share these examples? Please provide code if you have it. – slm Feb 10 '13 at 04:40
  • The answer is that they are not doing the same thing in your scenario. If you share the code, someone will gladly point out the exact difference :) – Sergey Kalinichenko Feb 10 '13 at 04:42
  • Hi, i will post a code but it is a simple just add function, returns a + b, if you create nested for loop and measure the time it runs, anonymous delegates runs almost 5 times faster. – Diggie Feb 10 '13 at 04:49
  • 1
    Without the code we have no idea what you're actually measuring. It's very easy to get micro benchmarks wrong, so please post the code. – Brian Rasmussen Feb 10 '13 at 04:53
  • 1
    The code above does not compile. We are intrigued by your question, please paste the actual code. – John Feb 10 '13 at 05:01
  • Sorry, i saw this example in youtube, if you run any delegates function with simple calculations, anonymous delegates performance is alot better than regular delegates. – Diggie Feb 10 '13 at 05:05
  • 1
    Have you actually tested this for your self? The above code still does not compile. Why don't you run it once then copy the text in full from your `.cs` file? – Scott Chamberlain Feb 10 '13 at 05:10
  • Until you can provide a working example I am going to give you a -1 as per the button says "This question does not show research effort". If you can come back after you have run the test yourself I will remove it, maybe even make it a +1. – Scott Chamberlain Feb 10 '13 at 05:16
  • 1
    Please compare my edit and your edit from code look perspective. How do you like it's new state, huh? – abatishchev Feb 10 '13 at 05:18
  • @abatishchev That code needs more than just indenting to look like good code to me, although fixing code from a question (barring whitespace) isn't something to be done in an edit. – Servy Feb 10 '13 at 05:24

2 Answers2

5

Whether or not a function has a name or not doesn't affect it's speed at all. It will be given a name by the compiler, it's just not one you can reference in your code. The question itself is flawed. Anonymous methods are not inherently faster than named methods.

If you have a benchmark that shows otherwise either the methods are not actually doing the same thing, or the benchmarking code is flawed and not accurately measuring the performance of one or both of the methods. Accurately measuring the performance of methods is very difficult in a language like C#, so flawed performance tests are very common, even for experienced programmers.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • When I'd done some benchmarks last time anonymous types were marginally slower than named types. – nawfal Feb 10 '13 at 05:33
2

That's not how delegates are actually used. You should pass the delegate to a separate function; this is the typical scenario for a delegate.

You also need a LOT more than 1000 iterations. Otherwise even a single interrupt occurring during your test can skew the results.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720