forked from angularsen/UnitsNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitKeyHashCodeBenchmarks.cs
More file actions
72 lines (61 loc) · 1.74 KB
/
Copy pathUnitKeyHashCodeBenchmarks.cs
File metadata and controls
72 lines (61 loc) · 1.74 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
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using UnitsNet.Units;
namespace UnitsNet.Benchmark.Enums;
// [MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net48)]
[SimpleJob(RuntimeMoniker.Net80)]
public class UnitKeyHashCodeBenchmarks
{
private const int NbIterations = 1000;
private static readonly UnitKey UnitKey = UnitKey.ForUnit(VolumeUnit.CubicMeter);
private readonly Type UnitType = UnitKey.UnitEnumType;
private readonly int UnitValue = UnitKey.UnitEnumValue;
[Benchmark(Baseline = true)]
public int GetHashCodeRecord()
{
int hashCode = 0;
for (var i = 0; i < NbIterations; i++)
{
hashCode += UnitKey.GetHashCode();
}
return hashCode;
}
[Benchmark]
public int GetCustomHashCode()
{
int hashCode = 0;
for (var i = 0; i < NbIterations; i++)
{
#if NET
hashCode += HashCode.Combine(UnitType, UnitValue);
#else
hashCode += (UnitType.GetHashCode() * 397) ^ UnitValue;
#endif
}
return hashCode;
}
[Benchmark]
public int GetCustomHashCodeUnchecked()
{
int hashCode = 0;
for (var i = 0; i < NbIterations; i++)
{
if (UnitType == null)
{
hashCode += UnitValue;
}
else
{
unchecked
{
hashCode += (UnitType.GetHashCode() * 397) ^ UnitValue;
}
}
}
return hashCode;
}
}