Nullable Versus Integral Data Types (C# 2.0 Test Code)

using System;
using System.Collections.Generic;

public class NullableVsValueType {
    const int keyBase = 10000000;
    const int keys = 1000000;
   
    private static void Main(string[] args) {
        string[] stringsForKeys = new string[keys];
        int counter = 0;
        Dictionary<string, int> intDictionary = new Dictionary<string, int>();
        Dictionary<string, Nullable<int>> nullableDictionary = new Dictionary<string, Nullable<int>>();
        Dictionary<int, int> iintDictionary = new Dictionary<int, int>();
        Dictionary<int, Nullable<int>> inullableDictionary = new Dictionary<int, Nullable<int>>();

        DateTime start, end;
       
        Console.WriteLine("Start Pre-Processing Strings");
        start = DateTime.Now;
        for(int i = keyBase; i < (keyBase + keys); i++) {
            stringsForKeys[i - keyBase] = i.ToString();
        }
        end = DateTime.Now;
        Console.WriteLine("Pre-Processing Strings Complete: {0}", end - start);
        Console.WriteLine();
       
        Console.WriteLine("Start Dictionary<String, Int> Dictionary");
        start = DateTime.Now; counter = 0;
        for(int i = 0; i < keys; i++) {
            intDictionary[stringsForKeys[i]] = i;
            // counter += intDictionary[stringsForKeys[i]];
        }
        end = DateTime.Now;
        Console.WriteLine("Int Dictionary Complete: {0}, {1}", end - start, counter);
        Console.WriteLine();
       
        Console.WriteLine("Start Dictionary<String, Nullable<Int>> Dictionary");
        start = DateTime.Now; counter = 0;
        for(int i = 0; i < keys; i++) {
            nullableDictionary[stringsForKeys[i]] = i;
            // nullableDictionary[stringsForKeys[i]] = new Nullable<int>(i);
            // counter += nullableDictionary[stringsForKeys[i]].Value;
        }
        end = DateTime.Now;
        Console.WriteLine("Nullable<Int> Dictionary Complete: {0}, {1}", end - start, counter);
        Console.WriteLine();

        Console.WriteLine("Start Dictionary<Int, Int> Dictionary");
        start = DateTime.Now; counter = 0;
        for(int i = 0; i < keys; i++) {
            iintDictionary[i] = i;
            // counter += intDictionary[stringsForKeys[i]];
        }
        end = DateTime.Now;
        Console.WriteLine("Int Dictionary Complete: {0}, {1}", end - start, counter);
        Console.WriteLine();
       
        Console.WriteLine("Start Dictionary<Int, Nullable<Int>> Dictionary");
        start = DateTime.Now; counter = 0;
        for(int i = 0; i < keys; i++) {
            inullableDictionary[i] = i;
            // nullableDictionary[stringsForKeys[i]] = new Nullable<int>(i);
            // counter += nullableDictionary[stringsForKeys[i]].Value;
        }
        end = DateTime.Now;
        Console.WriteLine("Nullable<Int> Dictionary Complete: {0}, {1}", end - start, counter);
        Console.WriteLine();
    }
}

Published Saturday, May 01, 2004 1:25 AM by Justin Rogers
Filed under:

Comments

Saturday, May 01, 2004 4:48 AM by Justin Rogers

# re: Nullable Versus Integral Data Types (C# 2.0 Test Code)

Running this code a few more times Dictionary<int, int> and Dictionary<int, Nullable<int>> are definitely close in performance. They've actually traded off in some tests putting one or the other at the faster speed depending on code layout within the class file (aka the JIT is having a huge impact, and is very inconsistent, so we'll have to wait for a later release for better numbers)

However, the Dictionary<String, int> simply performs poorly and I don't think there is any good reason for it. There isn't any casting, or boxing, or anything in the IL that would make it intrinsically different from the Nullable version and the Nullable version is actually doing extra work to convert the integer value into a Nullable<int>. The extra indirection through the op_Implicit call should actually make it slower.

When I started this I expected to examine how much worse the Nullable version was, and I was expecting it was only going to be a small marginal amount making the use of Nullable types acceptable. I didn't expect to find Nullable to be faster, nor should it be under any circumstances faster than the integral type.
Saturday, May 01, 2004 7:28 AM by TrackBack

# re: Generic Dictionary and the KeyNotFoundException. Examining speed of Nullable types versus integral types...

Leave a Comment

(required) 
(required) 
(optional)
(required)