-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionBenchmarkRunner.cs
More file actions
87 lines (70 loc) · 2.36 KB
/
CollectionBenchmarkRunner.cs
File metadata and controls
87 lines (70 loc) · 2.36 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
using NMF.Expressions;
using NMF.Expressions.Linq;
using NMF.ExtensibilityBenchmark.Collection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NMF.ExtensibilityBenchmark
{
internal class CollectionBenchmarkRunner : CollectionBenchmarkRunnerBatch
{
private INotifyValue<double> resultCache;
public CollectionBenchmarkRunner(int seed) : base(seed)
{
}
public override double GetResult()
{
return resultCache.Value;
}
public override void Initialize()
{
resultCache = Observable.Expression(() => root.Items.Average(i => i.Value));
}
}
internal class RecursiveCollectionBenchmarkRunner : CollectionBenchmarkRunnerBatch
{
private INotifyValue<double> resultCache;
public RecursiveCollectionBenchmarkRunner(int seed) : base(seed)
{
}
public override void Initialize()
{
var computeAverage = Observable.Func(((int, int) tuple) => ((double)tuple.Item1) / tuple.Item2);
var recurse = Observable.Recurse<int, (int, int), (int, int)>((rec, index, tuple) => index >= root.Items.Count ? tuple : Add(rec(index + 1, tuple), root.Items[index].Value));
resultCache = Observable.Expression(() => computeAverage.Evaluate(recurse.Evaluate(0, ValueTuple.Create(0, 0))));
}
private static (int, int) Add((int, int) before, int value)
{
return (before.Item1 + value, before.Item2 + 1);
}
public override double GetResult()
{
return resultCache.Value;
}
}
internal class CollectionBenchmarkRunnerBatch : BenchmarkRunner<Item>
{
protected readonly Root root = new Root();
public CollectionBenchmarkRunnerBatch(int seed) : base(seed)
{
}
public override double GetResult()
{
return root.Items.AsEnumerable().Average(i => i.Value);
}
public override void Initialize()
{
}
protected override Item AddNewItem(int value)
{
var item = new Item { Value = value };
root.Items.Add(item);
return item;
}
protected override void ChangeValue(Item item, int value)
{
item.Value = value;
}
}
}