-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathMapVsDictionaryBenchmarks.cs
More file actions
115 lines (100 loc) · 2.22 KB
/
MapVsDictionaryBenchmarks.cs
File metadata and controls
115 lines (100 loc) · 2.22 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
namespace Towel_Benchmarking;
[Tag(Program.Name, "Map vs Dictionary (Add)")]
[Tag(Program.OutputFile, nameof(MapVsDictionaryAddBenchmarks))]
public class MapVsDictionaryAddBenchmarks
{
[Params(10, 100, 1000, 10000)]
public int N;
[Benchmark]
public void MapDelegates()
{
IMap<int, int> map = MapHashLinked.New<int, int>();
for (int i = 0; i < N; i++)
{
map.Add(i, i);
}
}
[Benchmark]
public void MapStructs()
{
MapHashLinked<int, int, IntEquate, IntHash> map = new();
for (int i = 0; i < N; i++)
{
map.Add(i, i);
}
}
internal struct IntHash : IFunc<int, int>
{
public int Invoke(int a) => a;
}
internal struct IntEquate : IFunc<int, int, bool>
{
public bool Invoke(int a, int b) => a == b;
}
[Benchmark]
public void Dictionary()
{
System.Collections.Generic.Dictionary<int, int> dictionary = new();
for (int i = 0; i < N; i++)
{
dictionary.TryAdd(i, i);
}
}
}
[Tag(Program.Name, "Map vs Dictionary (Look Up)")]
[Tag(Program.OutputFile, nameof(MapVsDictionaryLookUpBenchmarks))]
public class MapVsDictionaryLookUpBenchmarks
{
[Params(10, 100, 1000, 10000)]
public int N;
internal IMap<int, int>? mapHashLinked;
internal IMap<int, int>? mapHashLinkedStructs;
internal System.Collections.Generic.Dictionary<int, int>? dictionary;
internal int temp;
[IterationSetup]
public void IterationSetup()
{
mapHashLinked = MapHashLinked.New<int, int>();
mapHashLinkedStructs = new MapHashLinked<int, int, IntEquate, IntHash>();
dictionary = new System.Collections.Generic.Dictionary<int, int>();
for (int i = 0; i < N; i++)
{
mapHashLinked.Add(i, i);
mapHashLinkedStructs.Add(i, i);
dictionary.Add(i, i);
}
}
public void Temp() => Console.Write(temp);
[Benchmark]
public void MapDelegates()
{
for (int i = 0; i < N; i++)
{
temp = mapHashLinked![i];
}
}
[Benchmark]
public void MapStructs()
{
for (int i = 0; i < N; i++)
{
temp = mapHashLinkedStructs![i];
}
}
internal struct IntHash : IFunc<int, int>
{
public int Invoke(int a) => a;
}
internal struct IntEquate : IFunc<int, int, bool>
{
public bool Invoke(int a, int b) => a == b;
}
[Benchmark]
public void Dictionary()
{
for (int i = 0; i < N; i++)
{
temp = dictionary![i];
}
}
}