I read this post and I tried to do the same but I came across a very strange behavior when comparing the del.DynamicINvoke(args) Vs. del(args)
Update
So after Jon and usr comments i post now new working code.
I'll really appreciate any help!
Code:
using System;
using System.Diagnostic;
using System.Threading;
namespace DynamicInvokeVsInvoke {
public class Program {
static void Main(string[] args) {
var objArgs = new object[] {100, 1.2345678 };
Action<int, double> a = (i, d) => {};
Action<object[]> action = o => a((int)o[0], (double)o[1]);
var sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000; i++)
a.DynamicInvoke(objArgs);
Console.WriteLine("Dynamic: " + sw.ElapsedMilliseconds);
sw.Stop();
sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000; i++)
action(objArgs);
Console.WriteLine("Invoke: " + sw.ElapsedMilliseconds);
sw.Stop();
}
}
}
Results:
When 'a' is empty method and the loop runs 1000000 times the DynamicInvoke took approximately 4000 ms and the direct Invoke took 20 ms
When i'm put in a Thread.Sleep(20) and the loop runs 1000 times then the DynamicInvoke and the direct Invoke took approximately 20 seconds
p.s. i can't copy/paste from vs for some reason so i write the code manually if you see syntax error please let me know