-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathToStringWithDefaultPrecisionBenchmarks.cs
56 lines (47 loc) · 1.52 KB
/
ToStringWithDefaultPrecisionBenchmarks.cs
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
using System;
using System.Globalization;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using UnitsNet.Units;
namespace UnitsNet.Benchmark.Conversions.ToString;
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net48)]
[SimpleJob(RuntimeMoniker.Net80)]
public class ToStringWithDefaultPrecisionBenchmarks
{
private static readonly double Value = 123.456;
private readonly Random _random = new(42);
private Mass[] _masses = [];
private VolumeFlow[] _volumeFlows = [];
[Params(1000)]
public int NbConversions { get; set; }
[Params("G", "S", "E", "N", "A")]
public string Format { get; set; }
[GlobalSetup(Target = nameof(MassToString))]
public void PrepareMassesToTest()
{
_masses = _random.GetRandomQuantities<Mass, MassUnit>(Value, Mass.Units, NbConversions).ToArray();
}
[GlobalSetup(Target = nameof(VolumeFlowToString))]
public void PrepareVolumeFlowsToTest()
{
_volumeFlows = _random.GetRandomQuantities<VolumeFlow, VolumeFlowUnit>(Value, VolumeFlow.Units, NbConversions).ToArray();
}
[Benchmark(Baseline = true)]
public void MassToString()
{
foreach (Mass quantity in _masses)
{
var result = quantity.ToString(Format, CultureInfo.InvariantCulture);
}
}
[Benchmark]
public void VolumeFlowToString()
{
foreach (VolumeFlow quantity in _volumeFlows)
{
var result = quantity.ToString(Format, CultureInfo.InvariantCulture);
}
}
}