Skip to content

Instantly share code, notes, and snippets.

@lahma
Last active January 16, 2018 16:15
Show Gist options
  • Save lahma/6a7698f75916e68cd1a7c7663868e05b to your computer and use it in GitHub Desktop.
Save lahma/6a7698f75916e68cd1a7c7663868e05b to your computer and use it in GitHub Desktop.
Dictionary performance comparison
public class MruPropertyCacheBenchmark
{
string[] stringArray;
private Dictionary<string, IPropertyDescriptor> _basicDictionary;
private Dictionary<string, IPropertyDescriptor> _ordinalDictionary;
private MruPropertyCache2<IPropertyDescriptor> _mruPropertyCache;
private StringMap<IPropertyDescriptor> _stringMap;
private MruPropertyCache<IPropertyDescriptor> _mruPropertyCacheLinked;
[GlobalSetup]
public void Setup()
{
stringArray = new string[N];
for (int i = 0; i < N; i++)
{
stringArray[i] = i.ToString();
}
_basicDictionary = new Dictionary<string, IPropertyDescriptor>();
_basicDictionary["first"] = PropertyDescriptor.Undefined;
_basicDictionary["second"] = PropertyDescriptor.Undefined;
_basicDictionary["third"] = PropertyDescriptor.Undefined;
SetValueDictionary();
_ordinalDictionary = new Dictionary<string, IPropertyDescriptor>(StringComparer.Ordinal);
_ordinalDictionary["first"] = PropertyDescriptor.Undefined;
_ordinalDictionary["second"] = PropertyDescriptor.Undefined;
_ordinalDictionary["third"] = PropertyDescriptor.Undefined;
SetValueDictionaryOrdinal();
_mruPropertyCache = new MruPropertyCache2<IPropertyDescriptor>();
_mruPropertyCache["first"] = PropertyDescriptor.Undefined;
_mruPropertyCache["second"] = PropertyDescriptor.Undefined;
_mruPropertyCache["third"] = PropertyDescriptor.Undefined;
SetValueMru();
_mruPropertyCacheLinked = new MruPropertyCache<IPropertyDescriptor>(4);
_mruPropertyCacheLinked["first"] = PropertyDescriptor.Undefined;
_mruPropertyCacheLinked["second"] = PropertyDescriptor.Undefined;
_mruPropertyCacheLinked["third"] = PropertyDescriptor.Undefined;
SetValueMruLinked();
_stringMap = new StringMap<IPropertyDescriptor>();
_stringMap["first"] = PropertyDescriptor.Undefined;
_stringMap["second"] = PropertyDescriptor.Undefined;
_stringMap["third"] = PropertyDescriptor.Undefined;
SetValueStringMap();
}
[Params(3, 5, 10)]
public int N { get; set; }
[Benchmark]
public void SetValueDictionary()
{
for (int i = 0; i < N; ++i)
{
_basicDictionary[stringArray[i]] = PropertyDescriptor.Undefined;
}
}
[Benchmark]
public void SetValueDictionaryOrdinal()
{
for (int i = 0; i < N; ++i)
{
_ordinalDictionary[stringArray[i]] = PropertyDescriptor.Undefined;
}
}
[Benchmark]
public void SetValueMru()
{
for (int i = 0; i < N; ++i)
{
_mruPropertyCache[stringArray[i]] = PropertyDescriptor.Undefined;
}
}
[Benchmark]
public void SetValueMruLinked()
{
for (int i = 0; i < N; ++i)
{
_mruPropertyCacheLinked[stringArray[i]] = PropertyDescriptor.Undefined;
}
}
[Benchmark]
public void SetValueStringMap()
{
for (int i = 0; i < N; ++i)
{
_stringMap[stringArray[i]] = PropertyDescriptor.Undefined;
}
}
[Benchmark]
public void TryGetValueDictionary()
{
for (int i = 0; i < N; ++i)
{
_basicDictionary.TryGetValue("first", out _);
_basicDictionary.TryGetValue("second", out _);
_basicDictionary.TryGetValue("third", out _);
}
}
[Benchmark]
public void TryGetValueDictionaryOrdinal()
{
for (int i = 0; i < N; ++i)
{
_ordinalDictionary.TryGetValue("first", out _);
_ordinalDictionary.TryGetValue("second", out _);
_ordinalDictionary.TryGetValue("third", out _);
}
}
[Benchmark]
public void TryGetValueMru()
{
for (int i = 0; i < N; ++i)
{
_mruPropertyCache.TryGetValue("first", out _);
_mruPropertyCache.TryGetValue("second", out _);
_mruPropertyCache.TryGetValue("third", out _);
}
}
[Benchmark]
public void TryGetValueMruLinked()
{
for (int i = 0; i < N; ++i)
{
_mruPropertyCacheLinked.TryGetValue("first", out _);
_mruPropertyCacheLinked.TryGetValue("second", out _);
_mruPropertyCacheLinked.TryGetValue("third", out _);
}
}
[Benchmark]
public void TryGetValueStringMap()
{
for (int i = 0; i < N; ++i)
{
_stringMap.TryGetValue("first", out _);
_stringMap.TryGetValue("second", out _);
_stringMap.TryGetValue("third", out _);
}
}
}
BenchmarkDotNet=v0.10.11, OS=Windows 10 Redstone 3 [1709, Fall Creators Update] (10.0.16299.192)
Processor=Intel Core i7-6820HQ CPU 2.70GHz (Skylake), ProcessorCount=8
Frequency=2648434 Hz, Resolution=377.5816 ns, Timer=TSC
.NET Core SDK=2.1.4
  [Host]     : .NET Core 2.0.5 (Framework 4.6.26020.03), 64bit RyuJIT
  DefaultJob : .NET Core 2.0.5 (Framework 4.6.26020.03), 64bit RyuJIT

Method N Mean Error StdDev
SetValueDictionary 3 50.31 ns 0.1865 ns 0.1557 ns
SetValueDictionaryOrdinal 3 74.18 ns 0.0556 ns 0.0434 ns
SetValueMru 3 148.00 ns 0.6456 ns 0.5040 ns
SetValueMruLinked 3 142.32 ns 0.4891 ns 0.4336 ns
SetValueStringMap 3 97.08 ns 0.6217 ns 0.5815 ns
TryGetValueDictionary 3 194.31 ns 0.6063 ns 0.5375 ns
TryGetValueDictionaryOrdinal 3 284.24 ns 3.4354 ns 2.8688 ns
TryGetValueMru 3 275.17 ns 1.9899 ns 1.6616 ns
TryGetValueMruLinked 3 423.79 ns 0.3089 ns 0.2889 ns
TryGetValueStringMap 3 204.27 ns 0.1316 ns 0.1167 ns
SetValueDictionary 5 88.38 ns 0.0562 ns 0.0498 ns
SetValueDictionaryOrdinal 5 129.31 ns 0.5825 ns 0.5449 ns
SetValueMru 5 231.63 ns 0.4784 ns 0.4475 ns
SetValueMruLinked 5 495.16 ns 1.9495 ns 1.8235 ns
SetValueStringMap 5 162.58 ns 2.4376 ns 2.0355 ns
TryGetValueDictionary 5 308.27 ns 7.2540 ns 8.6354 ns
TryGetValueDictionaryOrdinal 5 436.72 ns 1.7040 ns 1.5105 ns
TryGetValueMru 5 437.52 ns 2.4441 ns 1.9082 ns
TryGetValueMruLinked 5 810.19 ns 5.1190 ns 4.2746 ns
TryGetValueStringMap 5 339.61 ns 2.4526 ns 2.2942 ns
SetValueDictionary 10 171.54 ns 0.6028 ns 0.5638 ns
SetValueDictionaryOrdinal 10 251.65 ns 0.1398 ns 0.1239 ns
SetValueMru 10 447.43 ns 0.1968 ns 0.1841 ns
SetValueMruLinked 10 976.06 ns 2.8115 ns 2.6298 ns
SetValueStringMap 10 324.34 ns 0.1199 ns 0.0867 ns
TryGetValueDictionary 10 605.81 ns 12.3768 ns 18.1417 ns
TryGetValueDictionaryOrdinal 10 909.05 ns 1.0925 ns 1.0219 ns
TryGetValueMru 10 855.01 ns 1.3972 ns 1.1668 ns
TryGetValueMruLinked 10 1,603.16 ns 0.8716 ns 0.7727 ns
TryGetValueStringMap 10 665.96 ns 2.1804 ns 2.0395 ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment