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