With the recent changes to Nullables in v2.0, I wanted to go back to some old tests that had not been very satisfying when I ran them over a year ago.
One was comparing the speed of using a Nullable<T> and testing to see if has a value or not with HasValue to the speed of a just checking for either a null or default value in a system.type.
Here were my results from doing this in July 2004
You will see that a Nullable<Int32> test was much slower than Int32>0 and a Nullable<object> test was slower than object = = null (or IsNull in VB).
Now with the August 05 CTP, things have sped up. One thing to note is that you can’t use Nullable<T> with already nullable values as you could before. So now I am only testing against int.
Using the same 500,000 interations with a Nullable<int>, although the gap is shrinking, is still somewhat slower. Three tests gave these results
- Nullable<int>.HasValue vs. int>0
- 4.7668 ms 3.2459 ms
- 5.9834 ms 3.4426 ms
- 4.0203 ms 3.8122 ms
Still, I much prefer Nullable<T> over the other test. What do you test for with dates?
Don’t Forget: www.acehaid.org
Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!