binary-trees C# aot #6 program
source code
// 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.Threading.Tasks;
class BinaryTrees
{
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;
}
}
const int MinDepth = 4;
const int NoTasks = 4;
public static void Main(string[] args)
{
int maxDepth = args.Length == 0 ? 10
: Math.Max(MinDepth + 2, int.Parse(args[0]));
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 check = 0;
for (int i = n; i > 0; i--)
check += TreeNode.Create(depth).Check();
return check;
});
}
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;
}
Console.WriteLine(stretchTreeCheck.Result);
for (int i = 0; i < results.Length; i++)
Console.WriteLine(results[i]);
Console.WriteLine(longLivedText.Result);
}
}
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
.NET Core SDK 3.1.201
Host Version: 3.1.3; Commit: 4a9f85e9f8
<ServerGarbageCollection>true
<ConcurrentGarbageCollection>true
/p:PublishReadyToRun=true
/p:RuntimeIdentifier=linux-x64
Thu, 07 May 2020 21:49:59 GMT
MAKE:
cp binarytrees.csharpaot-6.csharpaot Program.cs
cp Include/csharpcore/tmp.csproj .
mkdir obj
cp Include/csharpcore/project.assets.json ./obj
/usr/bin/dotnet publish -c Release --no-restore --no-self-contained /p:PublishReadyToRun=true /p:RuntimeIdentifier=linux-x64
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
tmp -> /home/dunham/benchmarksgame_quadcore/binarytrees/tmp/bin/Release/netcoreapp3.1/linux-x64/tmp.dll
tmp -> /home/dunham/benchmarksgame_quadcore/binarytrees/tmp/bin/Release/netcoreapp3.1/linux-x64/publish/
9.44s to complete and log all make actions
COMMAND LINE:
./bin/Release/netcoreapp3.1/linux-x64/tmp 21
PROGRAM OUTPUT:
stretch tree of depth 22 check: 8388607
2097152 trees of depth 4 check: 65011712
524288 trees of depth 6 check: 66584576
131072 trees of depth 8 check: 66977792
32768 trees of depth 10 check: 67076096
8192 trees of depth 12 check: 67100672
2048 trees of depth 14 check: 67106816
512 trees of depth 16 check: 67108352
128 trees of depth 18 check: 67108736
32 trees of depth 20 check: 67108832
long lived tree of depth 21 check: 4194303