Performance Test – Dynamic method invocation in C# 4

Yan Cui

I help clients go faster for less using serverless technologies.

This article is brought to you by

I never fully recovered my workspace setup when I upgraded my laptop two years ago, and I still miss things today. If only I had known about Gitpod back then…

Learn more

By now you would have no doubt heard about the new dynamic type in C# 4 and how it has changed the way you write interop code in C#. As impressed as I am by it, I have been wondering just how much (if any) of a performance hit we’d have to pay to move away from the fast but strict type system we have had so far.

Since the very first release of the .Net framework we’ve had the ability to invoke a method dynamically using reflection, but this is SLOW, so what of the new dynamic type? With this in mind, I put together a quick test to see how the three ways of invoking a method differ in performance:

class Program
{
static void Main(string[] args)
{
const int maxAttempt = 1000000;
var stopwatch = new Stopwatch();

#region Normal Invocation
var prog = new Program();
stopwatch.Start();
for (int i = 0; i < maxAttempt; i++) { prog.Foo(); } stopwatch.Stop(); Console.WriteLine("Normal Invocation took {0} milliseconds", stopwatch.ElapsedMilliseconds); #endregion #region Using Reflection Type t = prog.GetType(); stopwatch.Restart(); for (int i = 0; i < maxAttempt; i++) { t.InvokeMember("Foo", BindingFlags.InvokeMethod, null, prog, new object[] { }); } stopwatch.Stop(); Console.WriteLine("Using reflection took {0} milliseconds", stopwatch.ElapsedMilliseconds); #endregion #region Dynamic Invocation dynamic dynamicProg = prog; stopwatch.Restart(); for (int i = 0; i < maxAttempt; i++) { dynamicProg.Foo(); } stopwatch.Stop(); Console.WriteLine("Dynamic Invocation took {0} milliseconds", stopwatch.ElapsedMilliseconds); #endregion Console.ReadKey(); } public void Foo() {} } [/code] The result? Well, unsurprisingly normal invocation wins hands down, but invoking a method on the dynamic type is surprisingly fast compared to invoking a method using reflection! image

Whenever you’re ready, here are 3 ways I can help you:

  1. Production-Ready Serverless: Join 20+ AWS Heroes & Community Builders and 1000+ other students in levelling up your serverless game. This is your one-stop shop for quickly levelling up your serverless skills.
  2. I help clients launch product ideas, improve their development processes and upskill their teams. If you’d like to work together, then let’s get in touch.
  3. Join my community on Discord, ask questions, and join the discussion on all things AWS and Serverless.

4 thoughts on “Performance Test – Dynamic method invocation in C# 4”

  1. btw to make the relection a lot faster use this:

    #region Using Val Reflection
    Type tv = prog.GetType();
    stopwatch.Restart();
    var method = tv.GetMethod(“Foo”);
    for (int i = 0; i < maxAttempt; i++)
    {
    method.Invoke(prog, new object[] { });
    }
    stopwatch.Stop();
    Console.WriteLine("Using val reflection took {0} milliseconds",
    stopwatch.ElapsedMilliseconds);
    #endregion

  2. theburningmonk

    @Val

    nice one, what sorta numbers did you get with invoking the method using the MethodInfo vs InvokeMember?

  3. Pingback: Dynamics | Various Talks

Leave a Comment

Your email address will not be published. Required fields are marked *