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;
}
}
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.