Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As Hans mentions in the comments on your question, the <code>Execute</code> method is so simple that it's almost certainly being inlined by the jitter in your "native" test.</p> <p>So what you're seeing isn't a comparison between a standard method call and a delegate invocation, but a comparison between an inlined <code>i * 2</code> operation and a delegate invocation. (And that <code>i * 2</code> operation probably boils down to just a single machine instruction, about as fast as you can get.)</p> <p>Make your <code>Execute</code> methods a bit more complicated to prevent inlining (and/or do it with the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimploptions.aspx" rel="nofollow"><code>MethodImplOptions.NoInlining</code></a> compiler hint); then you'll get a more realistic comparison between standard method calls and delegate invocations. Chances are that the difference will be negligible in most situations:</p> <pre><code>[MethodImpl(MethodImplOptions.NoInlining)] static int Execute(int i) { return ((i / 63.53) == 34.23) ? -1 : (i * 2); } public static volatile int Result; private static void Main(string[] args) { const int iterations = 100000000; { Result = Execute(42); // pre-jit var s = Stopwatch.StartNew(); for (int i = 0; i &lt; iterations; i++) { Result = Execute(i); } s.Stop(); Console.WriteLine("Native: " + s.ElapsedMilliseconds); } { Func&lt;int, int&gt; func; using (var cscp = new CSharpCodeProvider()) { var cp = new CompilerParameters { GenerateInMemory = true, CompilerOptions = @"/optimize" }; string src = @"public static class Foo { public static int Execute(int i) { return ((i / 63.53) == 34.23) ? -1 : (i * 2); } }"; var cr = cscp.CompileAssemblyFromSource(cp, src); var mi = cr.CompiledAssembly.GetType("Foo").GetMethod("Execute"); func = (Func&lt;int, int&gt;)Delegate.CreateDelegate(typeof(Func&lt;int, int&gt;), mi); } Result = func(42); // pre-jit var s = Stopwatch.StartNew(); for (int i = 0; i &lt; iterations; i++) { Result = func(i); } s.Stop(); Console.WriteLine("Dynamic delegate: " + s.ElapsedMilliseconds); } { Func&lt;int, int&gt; func = Execute; Result = func(42); // pre-jit var s = Stopwatch.StartNew(); for (int i = 0; i &lt; iterations; i++) { Result = func(i); } s.Stop(); Console.WriteLine("Delegate: " + s.ElapsedMilliseconds); } } </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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