-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.d
More file actions
109 lines (93 loc) · 2.07 KB
/
benchmark.d
File metadata and controls
109 lines (93 loc) · 2.07 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
98
99
100
101
102
103
104
105
106
107
108
109
module benchmark;
import std.algorithm : filter, map;
import std.array : array;
import std.conv : to;
import std.range : iota;
import std.stdio : writeln;
import std.string : split;
template Pow2(int N)
{
static if (N == 0)
{
enum Pow2 = 1;
}
else
{
enum Pow2 = 2 * Pow2!(N - 1);
}
}
long weighted(long value, int factor)
{
return value * factor + (value % (factor + 1));
}
string buildDataset(size_t rowCount, size_t width)
{
string result;
foreach (r; 0 .. rowCount)
{
foreach (c; 0 .. width)
{
result ~= ((r * 13 + c * 17 + 11) % 997).to!string;
if (c + 1 < width)
{
result ~= ":";
}
}
result ~= "\n";
}
return result;
}
enum dataset = buildDataset(720, 12);
long datasetChecksum()
{
long acc = 1469598103934665603L;
foreach (ch; dataset)
{
acc ^= cast(ubyte) ch;
acc *= 1099511628211L;
}
return acc;
}
enum checksum = datasetChecksum();
auto pipeline(R)(R values)
{
return values
.map!(v => weighted(v, cast(int) (v % 7 + 2)))
.filter!(v => (v & 1) == 0)
.array;
}
long parseAndAggregate(string input)
{
long total = 0;
foreach (line; input.split('\n'))
{
if (line.length == 0)
{
continue;
}
foreach (col; line.split(':'))
{
total += col.to!long;
}
}
return total;
}
void instantiatePipelines()
{
auto ints = iota(1, 6_000).array;
auto longs = iota(2L, 6_500L).array;
auto floats = iota(1.0, 4_000.0).array;
auto a = pipeline(ints);
auto b = pipeline(longs);
auto c = pipeline(floats.map!(x => cast(long) x).array);
assert(a.length + b.length + c.length > 0);
}
void main()
{
instantiatePipelines();
auto values = iota(1, 8_000).array;
auto reduced = pipeline(values);
auto aggregate = parseAndAggregate(dataset);
enum compileBias = Pow2!9 + checksum % 1024;
writeln("rows=", reduced.length, " aggregate=", aggregate + compileBias);
}