-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathbinarytrees-6.cs
More file actions
122 lines (103 loc) · 4.03 KB
/
binarytrees-6.cs
File metadata and controls
122 lines (103 loc) · 4.03 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
110
111
112
113
114
115
116
117
118
119
120
121
122
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// Adapted from binary-trees C# .NET Core #6 program
// Best-scoring C# .NET Core version as of 2020-08-12
// The Computer Language Benchmarks Game
// https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
//
// contributed by Marek Safar
// concurrency added by Peperud
// fixed long-lived tree by Anthony Lloyd
// ported from F# version by Anthony Lloyd
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace BenchmarksGame
{
[MaxIterationCount(40)] // the default 20 is not enough, the benchmark has multimodal distribution and needs more runs
[BenchmarkCategory(Categories.Runtime, Categories.BenchmarksGame, Categories.JIT, Categories.NoWASM)]
public class BinaryTrees_6
{
const int MinDepth = 4;
const int NoTasks = 4;
// 16 is about 80ms
// 18 is about 500ms
// 20 is about 3.9s
// 21 is used in official numbers; about 7.8s
const int N = 18;
[Benchmark(Description = nameof(BinaryTrees_6))]
public int RunBench() => Bench(N, verbose: false);
static int Bench(int param, bool verbose)
{
int maxDepth = Math.Max(MinDepth + 2, param);
var stretchTreeCheck = Task.Run(() =>
{
int stretchDepth = maxDepth + 1;
return "stretch tree of depth " + stretchDepth + "\t check: " +
TreeNode.Create(stretchDepth).Check();
});
var longLivedTree = TreeNode.Create(maxDepth);
var longLivedText = Task.Run(() =>
{
return "long lived tree of depth " + maxDepth +
"\t check: " + longLivedTree.Check();
});
var results = new string[(maxDepth - MinDepth) / 2 + 1];
for (int i = 0; i < results.Length; i++)
{
int depth = i * 2 + MinDepth;
int n = (1 << maxDepth - depth + MinDepth) / NoTasks;
var tasks = new Task<int>[NoTasks];
for (int t = 0; t < tasks.Length; t++)
{
tasks[t] = Task.Run(() =>
{
var check2 = 0;
for (int i2 = n; i2 > 0; i2--)
check2 += TreeNode.Create(depth).Check();
return check2;
});
}
var check = tasks[0].Result;
for (int t = 1; t < tasks.Length; t++)
check += tasks[t].Result;
results[i] = (n * NoTasks) + "\t trees of depth " + depth +
"\t check: " + check;
}
if (verbose)
{
Console.WriteLine(stretchTreeCheck.Result);
for (int i = 0; i < results.Length; i++)
Console.WriteLine(results[i]);
Console.WriteLine(longLivedText.Result);
}
return 0;
}
struct TreeNode
{
class Next { public TreeNode left, right; }
readonly Next next;
TreeNode(TreeNode left, TreeNode right) =>
next = new Next { left = left, right = right };
internal static TreeNode Create(int d)
{
return d == 1 ? new TreeNode(new TreeNode(), new TreeNode())
: new TreeNode(Create(d - 1), Create(d - 1));
}
internal int Check()
{
int c = 1;
var current = next;
while (current != null)
{
c += current.right.Check() + 1;
current = current.left.next;
}
return c;
}
}
}
}