Create a gist now

Instantly share code, notes, and snippets.

[Config(typeof(Config))]
public class FastTypeUShortDictionary
{
static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<FastTypeUShortDictionary>();
}
private class Config : ManualConfig
{
public Config()
{
Add(Job.Clr.WithLaunchCount(1).WithWarmupCount(5).WithTargetCount(5));
Add(JitOptimizationsValidator.FailOnError);
Add(RPlotExporter.Default);
}
}
public enum TestMode
{
Empty,
SingleItem,
Multiple,
};
public enum TestResultMode
{
Hit,
Miss
}
[Params(TestMode.Empty, TestMode.SingleItem, TestMode.Multiple)]
public TestMode Mode;
[Params(TestResultMode.Hit, TestResultMode.Miss)]
public TestResultMode ResultMode;
private Dictionary<Type, ushort> regularDictionary;
private Wire.FastTypeUShortDictionary fastDictionary;
private Type typeToLookup;
[Benchmark]
public bool DictionaryTryGet()
{
ushort ignored;
return regularDictionary.TryGetValue(typeToLookup, out ignored);
}
[Benchmark]
public bool FastDictionaryTryGet()
{
ushort ignored;
return fastDictionary.TryGetValue(typeToLookup, out ignored);
}
[Setup]
public void Setup()
{
if (ResultMode == TestResultMode.Hit)
typeToLookup = typeof(string); // Hit
else if (ResultMode == TestResultMode.Miss)
typeToLookup = typeof(Guid); //Miss
if (Mode == TestMode.Empty)
{
regularDictionary = new Dictionary<Type, ushort>();
fastDictionary = new Wire.FastTypeUShortDictionary();
}
else if (Mode == TestMode.SingleItem)
{
regularDictionary = new Dictionary<Type, ushort>()
{
{ typeof(string), 1 },
};
fastDictionary = new Wire.FastTypeUShortDictionary()
{
{ typeof(string), 1 },
};
}
else if (Mode == TestMode.Multiple)
{
regularDictionary = new Dictionary<Type, ushort>()
{
{ typeof(string), 1 },
{ typeof(int), 2 },
{ typeof(long), 3 },
{ typeof(short), 4 },
{ typeof(uint), 5 },
{ typeof(ulong), 6 },
{ typeof(ushort), 7 },
{ typeof(byte), 8 },
{ typeof(sbyte), 9 },
{ typeof(bool), 10 },
};
fastDictionary = new Wire.FastTypeUShortDictionary();
foreach (var item in regularDictionary)
fastDictionary.Add(item.Key, item.Value);
}
}
}
@mattwarren
Owner

Results:

image

@mattwarren
Owner
Host Process Environment Information:
BenchmarkDotNet.Core=v0.9.9.0
OS=Microsoft Windows NT 6.1.7601 Service Pack 1
Processor=Intel(R) Core(TM) i7-4800MQ CPU 2.70GHz, ProcessorCount=8
Frequency=2630761 ticks, Resolution=380.1181 ns, Timer=TSC
CLR=MS.NET 4.0.30319.42000, Arch=32-bit RELEASE
GC=Concurrent Workstation
JitModules=clrjit-v4.6.1076.0

Type=FastTypeUShortDictionary  Mode=Throughput  Toolchain=Clr  
Runtime=Clr  LaunchCount=1  WarmupCount=5  
TargetCount=5  
Method Mode ResultMode Median StdDev
DictionaryTryGet Empty Hit 7.2320 ns 0.3798 ns
FastDictionaryTryGet Empty Hit 5.7231 ns 0.2296 ns
DictionaryTryGet Empty Miss 7.9585 ns 0.7375 ns
FastDictionaryTryGet Empty Miss 5.7959 ns 0.1486 ns
DictionaryTryGet Multiple Hit 22.4792 ns 1.5588 ns
FastDictionaryTryGet Multiple Hit 22.5586 ns 1.4716 ns
DictionaryTryGet Multiple Miss 12.5840 ns 0.6674 ns
FastDictionaryTryGet Multiple Miss 16.8931 ns 2.0999 ns
DictionaryTryGet SingleItem Hit 21.0799 ns 1.3826 ns
FastDictionaryTryGet SingleItem Hit 6.1794 ns 0.2320 ns
DictionaryTryGet SingleItem Miss 15.8175 ns 0.5082 ns
FastDictionaryTryGet SingleItem Miss 6.2207 ns 0.2873 ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment