-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDistributionBenchmark.cs
More file actions
93 lines (84 loc) · 2.63 KB
/
Copy pathDistributionBenchmark.cs
File metadata and controls
93 lines (84 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using Celerity.Collections;
/// <summary>
/// Sweeps the integer dictionaries across <see cref="Distribution.Uniform"/>,
/// <see cref="Distribution.Sequential"/>, and <see cref="Distribution.Clustered"/>
/// key shapes for both 1k and 100k items, against the BCL
/// <see cref="Dictionary{TKey, TValue}"/> baseline.
/// </summary>
/// <remarks>
/// The original suite measured a single random distribution only, which hides the
/// whole reason Celerity ships tunable hashers: the cheap XOR-fold hashers win on
/// uniform/sequential keys and can lose on pathological ones. This benchmark makes
/// that trade visible. The <see cref="Distribution.Adversarial"/> shape is covered
/// separately by <see cref="AdversarialHasherBenchmark"/> because it is capped at
/// <see cref="KeyDistributions.MaxAdversarialCount"/> distinct keys.
/// </remarks>
[MemoryDiagnoser(false)]
[CategoriesColumn]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
public class DistributionBenchmark
{
private int[] keys = null!;
private Dictionary<int, int> dictionary = null!;
private IntDictionary<int> intDictionary = null!;
[Params(Distribution.Uniform, Distribution.Sequential, Distribution.Clustered)]
public Distribution Distribution;
[Params(1000, 100_000)]
public int ItemCount;
[GlobalSetup]
public void Setup()
{
keys = KeyDistributions.Int32(Distribution, ItemCount);
dictionary = new Dictionary<int, int>(ItemCount);
intDictionary = new IntDictionary<int>(ItemCount);
foreach (var key in keys)
{
dictionary[key] = key;
intDictionary[key] = key;
}
}
[Benchmark(Baseline = true)]
[BenchmarkCategory("Insert")]
public void Dictionary_Insert()
{
var map = new Dictionary<int, int>();
foreach (var key in keys)
{
map[key] = key;
}
}
[Benchmark]
[BenchmarkCategory("Insert")]
public void IntDictionary_Insert()
{
var map = new IntDictionary<int>();
foreach (var key in keys)
{
map[key] = key;
}
}
[Benchmark(Baseline = true)]
[BenchmarkCategory("Lookup")]
public int Dictionary_Lookup()
{
int acc = 0;
foreach (var key in keys)
{
acc += dictionary[key];
}
return acc;
}
[Benchmark]
[BenchmarkCategory("Lookup")]
public int IntDictionary_Lookup()
{
int acc = 0;
foreach (var key in keys)
{
acc += intDictionary[key];
}
return acc;
}
}