Last active
February 22, 2020 22:37
-
-
Save BrandonLWhite/d823dcbddc256be6fd879b7be7d3960f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Diagnostics; | |
namespace SwitchVsDictionary | |
{ | |
static class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var typeCodes = TypeCodeToTypeMap.Keys.ToList(); | |
int iterations = 10000000; | |
Benchmark(() => | |
{ | |
foreach(var typeCode in typeCodes) | |
{ | |
typeCode.ToTypeDict(); | |
} | |
}, iterations); | |
Benchmark(() => | |
{ | |
foreach (var typeCode in typeCodes) | |
{ | |
typeCode.ToTypeSwitch(); | |
} | |
}, iterations); | |
} | |
/// <summary> | |
/// Table that maps TypeCode to it's corresponding Type. | |
/// </summary> | |
static IReadOnlyDictionary<TypeCode, Type> TypeCodeToTypeMap = new Dictionary<TypeCode, Type> | |
{ | |
{ TypeCode.Boolean, typeof(bool) }, | |
{ TypeCode.Byte, typeof(byte) }, | |
{ TypeCode.Char, typeof(char) }, | |
{ TypeCode.DateTime, typeof(DateTime) }, | |
{ TypeCode.DBNull, typeof(DBNull) }, | |
{ TypeCode.Decimal, typeof(decimal) }, | |
{ TypeCode.Double, typeof(double) }, | |
{ TypeCode.Empty, null }, | |
{ TypeCode.Int16, typeof(short) }, | |
{ TypeCode.Int32, typeof(int) }, | |
{ TypeCode.Int64, typeof(long) }, | |
{ TypeCode.Object, typeof(object) }, | |
{ TypeCode.SByte, typeof(sbyte) }, | |
{ TypeCode.Single, typeof(Single) }, | |
{ TypeCode.String, typeof(string) }, | |
{ TypeCode.UInt16, typeof(UInt16) }, | |
{ TypeCode.UInt32, typeof(UInt32) }, | |
{ TypeCode.UInt64, typeof(UInt64) } | |
}; | |
/// <summary> | |
/// Convert a TypeCode ordinal into it's corresponding Type instance. | |
/// </summary> | |
public static Type ToTypeDict(this TypeCode code) | |
{ | |
return TypeCodeToTypeMap[code]; | |
} | |
public static Type ToTypeSwitch(this TypeCode code) | |
{ | |
switch (code) | |
{ | |
case TypeCode.Boolean: | |
return typeof(bool); | |
case TypeCode.Byte: | |
return typeof(byte); | |
case TypeCode.Char: | |
return typeof(char); | |
case TypeCode.DateTime: | |
return typeof(DateTime); | |
case TypeCode.DBNull: | |
return typeof(DBNull); | |
case TypeCode.Decimal: | |
return typeof(decimal); | |
case TypeCode.Double: | |
return typeof(double); | |
case TypeCode.Empty: | |
return null; | |
case TypeCode.Int16: | |
return typeof(short); | |
case TypeCode.Int32: | |
return typeof(int); | |
case TypeCode.Int64: | |
return typeof(long); | |
case TypeCode.Object: | |
return typeof(object); | |
case TypeCode.SByte: | |
return typeof(sbyte); | |
case TypeCode.Single: | |
return typeof(Single); | |
case TypeCode.String: | |
return typeof(string); | |
case TypeCode.UInt16: | |
return typeof(UInt16); | |
case TypeCode.UInt32: | |
return typeof(UInt32); | |
case TypeCode.UInt64: | |
return typeof(UInt64); | |
} | |
return null; | |
} | |
/// <summary> | |
/// From http://stackoverflow.com/questions/1622440/benchmarking-method-calls-in-c-sharp | |
/// </summary> | |
private static void Benchmark(Action act, int iterations) | |
{ | |
GC.Collect(); | |
Stopwatch sw = Stopwatch.StartNew(); | |
for (int i = 0; i < iterations; i++) | |
{ | |
act.Invoke(); | |
} | |
sw.Stop(); | |
Console.WriteLine(sw.ElapsedMilliseconds); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment