Search
Close this search box.

C# 4.0 Dynamics vs. Reflection

Yes, this is possibly the simplest app ever written and was cooked up as a lunch bet between myself (@sundriedcoder) and David Justice (@davidjustice) while waiting on our TFS server to be rebuilt.  Goal, set a property one million times.  Ah ha, here’s the catch!  Do it once with Reflection, once using C# 4.0 dynamics and once using a plain old property setter (POPS – I just made this acronym up).  The results just might amaze and frighten you.  See below or just copy and paste into your VS 2010 console app and hit F5.

What will this code do?

   1:      class Program
   2:      {
   3:          static void Main(string[] args)
   4:          {
   5:              var nTimes = 1000000;
   6:              var value = "Hello World";
   7:              var myTestType = new MyTestType();
   8:              DateTime start, end;
   9:              var property = typeof(MyTestType).GetProperty("MyDynamicProperty");
  10:   
  11:              start = DateTime.Now;
  12:              for (var i = 0; i < nTimes; i++)
  13:              {
  14:                  //var property = typeof(MyTestType).GetProperty("MyDynamicProperty");
  15:                  property.SetValue(myTestType, value, null);
  16:              }
  17:              end = DateTime.Now;
  18:              Console.WriteLine(end.Subtract(start).ToString());
  19:   
  20:              dynamic myTestType2 = new MyTestType();
  21:              start = DateTime.Now;
  22:              for (int i = 0; i < nTimes; i++)
  23:              {
  24:                  myTestType2.MyDynamicProperty = value;
  25:              }
  26:              end = DateTime.Now;
  27:              Console.WriteLine(end.Subtract(start).ToString());
  28:   
  29:              var myTestType3 = new MyTestType();
  30:              start = DateTime.Now;
  31:              for (int i = 0; i < nTimes; i++)
  32:              {
  33:                  myTestType3.MyDynamicProperty = value;
  34:              }
  35:              end = DateTime.Now;
  36:              Console.WriteLine(end.Subtract(start).ToString());
  37:              
  38:              Console.ReadLine();
  39:          }
  40:   
  41:          public class MyTestType
  42:          {
  43:              public string MyDynamicProperty { get; set; }
  44:          }
  45:      }

RESULTS

Here are the results as run on my machine.  Your results may vary but probably not by much.

  • Reflection: 20 seconds
  • Dynamics: .6 second
  • Normal Property Setter: .01 second

FINAL VERDICT

  • As expected, using a plain old property setter runs extremely fast in .01 second
  • C# 4.0’s dynamic data type comes in second place at .6 second.  This is 60 times slower than using a normal property setter but still, not that bad!
  • Reflection comes in WAY last with 20 seconds.  That is 2000 times slower than using a normal property setter!

My take away from this is that performance is probably not a good reason to avoid using dynamics if it saves you from having to write a lot of code.  Of course, dynamics are a tool that should be applied wisely, aka: Use at your own risk.  I hope you enjoy this post, I will be enjoying my free lunch.  Thanks Dave 😉

This article is part of the GWB Archives. Original Author: Kevin Rohling

Related Posts