tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Nullable types – what’s faster? – part 2

23 Sep 2013 2 mins .NET

Years ago I wrote post Nullable types – what’s faster? at that time on .NET FW 3.5. Today I came across post Hrátky s Nullable typy which is related to my old post. That made me thinking about the test again and how it’s the state today.

So I redid the test. This time on .NET FW 4.5 and directly on Intel Core i7 on my laptop not in VPC. Again build with full optimizations turned on and debugger not attached. I also instead of using short? used int? because I think it’s datatype that we, developers, use most often. I did couple of runs (about 100) removing some values that were obviously off and I also checked the generated IL, because that’s pretty closely estimates what’s actually going on.

HasValue vs. != null

No difference in time (all times in a margin of error) and also the IL code is same.

Foo.Value vs. (int)Foo

No difference in time (all times in a margin of error) and also the IL code is same.

I don’t know whether the C# 5.0 compiler now generates new (better) code or the old results were skewed because of some other influence (i.e. running on different machine (VPC actually)). But it probably doesn’t matter. Important is the code is same and you don’t have to think how to write it (although it’s a micro-optimization, probably premature). Write what you like more. 😃

If you have different results use comments and tell us (with some details about environment).

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at