I was reading about the new C# 5.0 language feature CallerInfoAttribute and how it can used to remove the 'magic strings' when using INotifyPropertyChanged - here. I've been avoiding the use of 'magic strings' for some time by the use of expression trees. I wanted to compare performance for CallerInfoAttribute versus expression trees.
So my pre C# 5.0 way to do this would have looked like this in a model class:
So my pre C# 5.0 way to do this would have looked like this in a model class:
Where the expression tree helper looks something like this:
So as you can see I've removed the need for 'magic strings'. As I said I was wondering how this would perform against the new language feature CallerInfoAttribute, yuou would typical use this like the following for INotifyPropertyChanged calls:
So comparing performance was just case of knocking up a quick model with debug statements, as you can see I'm just raising the property changed events the two different ways. Each way is timed and the number of ticks debugged out:
I ran this test from the test runner inside the VS 2011 beta. I set the properties twice to remove an ambiguity of 'warming up' the framework:
The output below shows I'm getting a performance increase by a factor of 6 using CallInfoAttribute :)
Not sure about the new metro styled visual studio though...
Comments
Post a Comment