-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListBenchmarkRunner.cs
More file actions
89 lines (76 loc) · 2.44 KB
/
LinkedListBenchmarkRunner.cs
File metadata and controls
89 lines (76 loc) · 2.44 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
using NMF.Expressions;
using NMF.ExtensibilityBenchmark.LinkedList;
using System;
using System.Collections.Generic;
using System.Text;
namespace NMF.ExtensibilityBenchmark
{
internal class LinkedListBenchmarkRunner : LinkedListBenchmarkRunnerBatch
{
private INotifyValue<double> resultCache;
public LinkedListBenchmarkRunner(int seed) : base(seed)
{
}
public override double GetResult()
{
return resultCache.Value;
}
public override void Initialize()
{
var computeAverage = Observable.Func(((int, int) tuple) => ((double)tuple.Item1) / tuple.Item2);
var recurse = Observable.Recurse<IItem, (int, int), (int, int)>((rec, item, tuple) => item == null ? tuple : Add(rec(item.Next, tuple), item.Value));
resultCache = Observable.Expression(() => computeAverage.Evaluate(recurse.Evaluate(root.Head, ValueTuple.Create(0,0))));
resultCache.Successors.SetDummy();
}
private static (int, int) Add((int, int) before, int value)
{
return (before.Item1 + value, before.Item2 + 1);
}
}
internal class LinkedListBenchmarkRunnerBatch : BenchmarkRunner<Item>
{
protected readonly Root root = new Root();
public LinkedListBenchmarkRunnerBatch(int seed) : base(seed)
{
}
public override double GetResult()
{
var sum = 0;
var count = 0;
var current = root.Head;
while (current != null)
{
sum += current.Value;
count++;
current = current.Next;
}
return ((double)sum) / count;
}
public override void Initialize()
{
}
private static (int, int) Add((int, int) before, int value)
{
return (before.Item1 + value, before.Item2 + 1);
}
protected override Item AddNewItem(int value)
{
var item = new Item { Value = value };
if (root.Tail == null)
{
root.Head = item;
root.Tail = item;
}
else
{
root.Tail.Next = item;
root.Tail = item;
}
return item;
}
protected override void ChangeValue(Item item, int value)
{
item.Value = value;
}
}
}