|
// This benchmark use one record and TryGetValue returns true. |
|
// Results will be affected by record num and whether match any record or not. |
|
|
|
using System.Collections.Concurrent; |
|
using BenchmarkDotNet.Attributes; |
|
using BenchmarkDotNet.Running; |
|
|
|
BenchmarkRunner.Run<ConcurrentDictionaryBench>(); |
|
|
|
record C1(string a, int b, string c); |
|
record struct S2(string a, int b); |
|
record C2(string a, int b); |
|
[MemoryDiagnoser] |
|
// [ShortRunJob] |
|
public class ConcurrentDictionaryBench |
|
{ |
|
private static readonly ConcurrentDictionary<(string, int), C1> dic1 = new(); |
|
private static readonly ConcurrentDictionary<string, C1> dic2 = new(); |
|
private static readonly ConcurrentDictionary<Tuple<string, int>, C1> dic3 = new(); |
|
private static readonly ConcurrentDictionary<S2, C1> dic4 = new(); |
|
private static readonly ConcurrentDictionary<C2, C1> dic5 = new(); |
|
private static readonly C1 _instance = new C1("hoge", 1, "piyo"); |
|
[GlobalSetup] |
|
public void Setup() |
|
{ |
|
dic1[(_instance.a, _instance.b)] = _instance; |
|
dic2[$"{_instance.a}_{_instance.b}"] = _instance; |
|
dic3[Tuple.Create(_instance.a, _instance.b)] = _instance; |
|
dic4[new S2(_instance.a, _instance.b)] = _instance; |
|
dic5[new C2(_instance.a, _instance.b)] = _instance; |
|
} |
|
[Benchmark] |
|
public void VTuple() |
|
{ |
|
dic1.TryGetValue((_instance.a, _instance.b), out var val); |
|
} |
|
[Benchmark] |
|
public void CombinedString() |
|
{ |
|
dic2.TryGetValue($"{_instance.a}_{_instance.b}", out var val); |
|
} |
|
[Benchmark] |
|
public void ClassTuple() |
|
{ |
|
dic3.TryGetValue(Tuple.Create(_instance.a, _instance.b), out var val); |
|
} |
|
[Benchmark] |
|
public void RecordStruct() |
|
{ |
|
dic4.TryGetValue(new S2(_instance.a, _instance.b), out var val); |
|
} |
|
[Benchmark] |
|
public void RecordClass() |
|
{ |
|
dic5.TryGetValue(new C2(_instance.a, _instance.b), out var val); |
|
} |
|
} |
|
|