-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathCurveBenchmarks.cs
More file actions
97 lines (79 loc) · 2.98 KB
/
CurveBenchmarks.cs
File metadata and controls
97 lines (79 loc) · 2.98 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
using BenchmarkDotNet.Attributes;
namespace FixedMathSharp.Benchmarks;
[MemoryDiagnoser]
public class CurveBenchmarks
{
private readonly Fixed64[] _times = BenchmarkFixtures.CurveTimes;
private readonly FixedCurve[] _linearCurves = BenchmarkFixtures.LinearCurves;
private readonly FixedCurve[] _stepCurves = BenchmarkFixtures.StepCurves;
private readonly FixedCurve[] _smoothCurves = BenchmarkFixtures.SmoothCurves;
private readonly FixedCurve[] _cubicCurves = BenchmarkFixtures.CubicCurves;
private readonly FixedCurve[] _linearTwoKeyCurves = CreateCurves(FixedCurveMode.Linear, 2);
private readonly FixedCurve[] _linearThirtyTwoKeyCurves = CreateCurves(FixedCurveMode.Linear, 32);
[Benchmark]
public Fixed64 LinearEvaluate()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _linearCurves.Length; i++)
accumulator += _linearCurves[i].Evaluate(_times[i]);
return accumulator;
}
[Benchmark]
public Fixed64 StepEvaluate()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _stepCurves.Length; i++)
accumulator += _stepCurves[i].Evaluate(_times[i]);
return accumulator;
}
[Benchmark]
public Fixed64 SmoothEvaluate()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _smoothCurves.Length; i++)
accumulator += _smoothCurves[i].Evaluate(_times[i]);
return accumulator;
}
[Benchmark]
public Fixed64 CubicEvaluate()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _cubicCurves.Length; i++)
accumulator += _cubicCurves[i].Evaluate(_times[i]);
return accumulator;
}
[Benchmark]
public Fixed64 LinearEvaluateTwoKeys()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _linearTwoKeyCurves.Length; i++)
accumulator += _linearTwoKeyCurves[i].Evaluate(Fixed64.Half);
return accumulator;
}
[Benchmark]
public Fixed64 LinearEvaluateThirtyTwoKeys()
{
Fixed64 accumulator = Fixed64.Zero;
for (int i = 0; i < _linearThirtyTwoKeyCurves.Length; i++)
accumulator += _linearThirtyTwoKeyCurves[i].Evaluate(_times[i]);
return accumulator;
}
private static FixedCurve[] CreateCurves(FixedCurveMode mode, int keyCount)
{
var curves = new FixedCurve[BenchmarkFixtures.SampleCount];
for (int i = 0; i < curves.Length; i++)
curves[i] = new FixedCurve(mode, CreateKeys(i, keyCount));
return curves;
}
private static FixedCurveKey[] CreateKeys(int seed, int keyCount)
{
var keys = new FixedCurveKey[keyCount];
for (int i = 0; i < keys.Length; i++)
{
Fixed64 time = new Fixed64(i);
Fixed64 value = new Fixed64((seed + i) % 17) + Fixed64.FromFraction(i, keyCount);
keys[i] = new FixedCurveKey(time, value, Fixed64.Half, Fixed64.Half);
}
return keys;
}
}