Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>Update: Added precompiled and lazy-compiled benchmarks</em></p> <p><em>Update 2: Turns out, I'm wrong. See Eric Lippert's post for a complete and correct answer. I'm leaving this here for the sake of the benchmark numbers</em></p> <p>*Update 3: Added IL-Emitted and Lazy IL-Emitted benchmarks, based on <a href="https://stackoverflow.com/questions/7531297/what-is-the-fastest-way-in-net-3-5-to-call-a-method-by-string-name/7531404#7531404">Mark Gravell's answer to this question</a>.</p> <p><strike>To my knowledge, use of the <code>dynamic</code> keyword does not cause any extra compilation at runtime in and of itself (though I imagine it could do so under specific circumstances, depending on what type of objects are backing your dynamic variables).</strike></p> <p>Regarding performance, <code>dynamic</code> does inherently introduce some overhead, but not nearly as much as you might think. For example, I just ran a benchmark that looks like this:</p> <pre><code>void Main() { Foo foo = new Foo(); var args = new object[0]; var method = typeof(Foo).GetMethod("DoSomething"); dynamic dfoo = foo; var precompiled = Expression.Lambda&lt;Action&gt;( Expression.Call(Expression.Constant(foo), method)) .Compile(); var lazyCompiled = new Lazy&lt;Action&gt;(() =&gt; Expression.Lambda&lt;Action&gt;( Expression.Call(Expression.Constant(foo), method)) .Compile(), false); var wrapped = Wrap(method); var lazyWrapped = new Lazy&lt;Func&lt;object, object[], object&gt;&gt;(() =&gt; Wrap(method), false); var actions = new[] { new TimedAction("Direct", () =&gt; { foo.DoSomething(); }), new TimedAction("Dynamic", () =&gt; { dfoo.DoSomething(); }), new TimedAction("Reflection", () =&gt; { method.Invoke(foo, args); }), new TimedAction("Precompiled", () =&gt; { precompiled(); }), new TimedAction("LazyCompiled", () =&gt; { lazyCompiled.Value(); }), new TimedAction("ILEmitted", () =&gt; { wrapped(foo, null); }), new TimedAction("LazyILEmitted", () =&gt; { lazyWrapped.Value(foo, null); }), }; TimeActions(1000000, actions); } class Foo{ public void DoSomething(){} } static Func&lt;object, object[], object&gt; Wrap(MethodInfo method) { var dm = new DynamicMethod(method.Name, typeof(object), new Type[] { typeof(object), typeof(object[]) }, method.DeclaringType, true); var il = dm.GetILGenerator(); if (!method.IsStatic) { il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Unbox_Any, method.DeclaringType); } var parameters = method.GetParameters(); for (int i = 0; i &lt; parameters.Length; i++) { il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Ldc_I4, i); il.Emit(OpCodes.Ldelem_Ref); il.Emit(OpCodes.Unbox_Any, parameters[i].ParameterType); } il.EmitCall(method.IsStatic || method.DeclaringType.IsValueType ? OpCodes.Call : OpCodes.Callvirt, method, null); if (method.ReturnType == null || method.ReturnType == typeof(void)) { il.Emit(OpCodes.Ldnull); } else if (method.ReturnType.IsValueType) { il.Emit(OpCodes.Box, method.ReturnType); } il.Emit(OpCodes.Ret); return (Func&lt;object, object[], object&gt;)dm.CreateDelegate(typeof(Func&lt;object, object[], object&gt;)); } </code></pre> <p>As you can see from the code, I try to invoke a simple no-op method seven different ways:</p> <ol> <li>Direct method call</li> <li>Using <code>dynamic</code></li> <li>By reflection</li> <li>Using an <code>Action</code> that got precompiled at runtime (thus excluding compilation time from the results).</li> <li>Using an <code>Action</code> that gets compiled the first time it is needed, using a non-thread-safe Lazy variable (thus including compilation time)</li> <li>Using a dynamically-generated method that gets created before the test.</li> <li>Using a dynamically-generated method that gets lazily instantiated during the test.</li> </ol> <p>Each gets called 1 million times in a simple loop. Here are the timing results:</p> <blockquote> <p>Direct: 3.4248ms<br> Dynamic: 45.0728ms<br> Reflection: 888.4011ms<br> Precompiled: 21.9166ms<br> LazyCompiled: 30.2045ms<br> ILEmitted: 8.4918ms<br> LazyILEmitted: 14.3483ms </p> </blockquote> <p>So while using the <code>dynamic</code> keyword takes an order of magnitude longer than calling the method directly, it still manages to complete the operation a million times in about 50 milliseconds, making it far faster than reflection. If the method we call were trying to do something intensive, like combining a few strings together or searching a collection for a value, those operations would likely far outweigh the difference between a direct call and a <code>dynamic</code> call.</p> <p>Performance is just one of many good reasons not to use <code>dynamic</code> unnecessarily, but when you're dealing with truly <code>dynamic</code> data, it can provide advantages that far outweigh the disadvantages.</p> <h2>Update 4</h2> <p>Based on Johnbot's comment, I broke the Reflection area down into four separate tests:</p> <pre><code> new TimedAction("Reflection, find method", () =&gt; { typeof(Foo).GetMethod("DoSomething").Invoke(foo, args); }), new TimedAction("Reflection, predetermined method", () =&gt; { method.Invoke(foo, args); }), new TimedAction("Reflection, create a delegate", () =&gt; { ((Action)method.CreateDelegate(typeof(Action), foo)).Invoke(); }), new TimedAction("Reflection, cached delegate", () =&gt; { methodDelegate.Invoke(); }), </code></pre> <p>... and here are the benchmark results:</p> <p><a href="https://i.stack.imgur.com/ufSE0.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ufSE0.png" alt="enter image description here"></a></p> <p>So if you can predetermine a specific method that you'll need to call a lot, invoking a cached delegate referring to that method is about as fast as calling the method itself. However, if you need to determine which method to call just as you're about to invoke it, creating a delegate for it is very expensive.</p>
    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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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