Geeks With Blogs
Kevin Rohling Building great s0ftware, 1 line at a time.

 

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 ;)

Posted on Friday, June 26, 2009 3:48 PM | Back to top


Comments on this post: C# 4.0 Dynamics vs. Reflection

# re: C# 4.0 Dynamics vs. Reflection
Requesting Gravatar...
Interestingly enough, if I change nTimes, the results change on my PC:

nTimes = 1
00:00:00.0010001 - Reflection
00:00:00.0230023 - Dynamic
00:00:00 - POPS

nTimes = 100
00:00:00.0010001 - Reflection
00:00:00.0220022 - Dynamic
00:00:00 - POPS

nTimes = 1000
00:00:00.0020002 - Reflection
00:00:00.0290029 - Dynamic
00:00:00 - POPS

nTimes = 10000
00:00:00.0160016 - Reflection
00:00:00.0300030 - Dynamic
00:00:00 - POPS

nTimes = 100000
00:00:00.1370137 - Reflection
00:00:00.0390039 - Dynamic
00:00:00.0040004 - POPS

nTimes = 1000000
00:00:01.4641464 - Reflection
00:00:00.0610061 - Dynamic
00:00:00.0090009 - POPS

nTimes = 10000000
00:00:13.5723571 - Reflection
00:00:00.4030403 - Dynamic
00:00:00.0890089 - POPS
Left by Ryan Haney on Nov 06, 2010 2:08 AM

# re: C# 4.0 Dynamics vs. Reflection
Requesting Gravatar...
Very interesting, would have thought reflection would have been faster.
Left by alex on Apr 12, 2011 1:53 PM

# re: C# 4.0 Dynamics vs. Reflection
Requesting Gravatar...
Hi Kevin, I was exactly looking to do the same test to replace some reflection code with generic (for clean up) but wanted to know first if it was faster. Your results clearly show that it is better with dynamic than reflection but doing the same test on my machine, I got really different results:

Reflection : 1.17 seconds
Dynamics : 0.07 second
Normal property setter : 0.0078 second

How can you get 20 seconds when I got 1.17 seconds?
Left by Ben on Oct 13, 2011 9:44 AM

Your comment:
 (will show your gravatar)


Copyright © SundriedCoder | Powered by: GeeksWithBlogs.net