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