-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchmarkRunner.cs
96 lines (82 loc) · 2.73 KB
/
BenchmarkRunner.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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace NMF.ExtensibilityBenchmark
{
internal interface IBenchmarkRunner
{
void Initialize();
void Generate(int size);
(double, double)[] Run();
}
internal abstract class BenchmarkRunner<T> : IBenchmarkRunner
{
private Random random;
private List<T> items = new List<T>();
private static Stopwatch stopwatch = new Stopwatch();
protected BenchmarkRunner(int seed)
{
random = new Random(seed);
}
protected abstract T AddNewItem(int value);
protected abstract void ChangeValue(T item, int value);
public abstract void Initialize();
public abstract double GetResult();
public (double, double)[] Run()
{
const int changes = 20;
// first one is to get JIT compiler to compile everything
for (int change = 0; change < changes; change++)
{
if (random.NextDouble() < 0.5)
{
items.Add(AddNewItem(random.Next(100)));
}
else
{
ChangeValue(items[random.Next(items.Count)], random.Next(100));
}
}
GetResult();
var result = new(double, double)[20];
for (int i = 0; i < result.Length; i++)
{
var indices = new int[changes];
var dices = new double[changes];
var values = new int[changes];
for (int j = 0; j < values.Length; j++)
{
indices[j] = random.Next(items.Count);
dices[j] = random.NextDouble();
values[j] = random.Next(100);
}
var runResult = 0.0;
stopwatch.Restart();
for (int change = 0; change < changes; change++)
{
if (dices[change] < 0.5)
{
items.Add(AddNewItem(values[change]));
}
else
{
ChangeValue(items[indices[change]], values[change]);
}
runResult += GetResult();
}
stopwatch.Stop();
result[i] = (stopwatch.Elapsed.TotalMilliseconds, runResult);
}
return result;
}
public void Generate(int size)
{
items.Clear();
for (int i = 0; i < size; i++)
{
items.Add(AddNewItem(random.Next(100)));
}
}
}
}