binary-trees C# .NET #8 program
source code
/* The Computer Language Benchmarks Game
https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
contributed by Isaac Gouy
*/
class Tree {
Tree left, right;
Tree(Tree left, Tree right){
this.left = left;
this.right = right;
}
internal static Tree with(int depth){
return (depth == 0)
? new Tree(null!, null!)
: new Tree(with(depth-1), with(depth-1));
}
internal int nodeCount(){
return (left == null)
? 1
: 1 + left.nodeCount() + right.nodeCount();
}
internal void clear(){
if (left != null) {
left.clear();
left = null!;
right.clear();
right = null!;
}
}
}
class BinaryTrees {
public static void Main(String[] args){
int n = 10;
if (args.Length > 0) n = Int32.Parse(args[0]);
int minDepth = 4;
int maxDepth = (minDepth + 2 > n) ? minDepth + 2 : n;
int stretchDepth = maxDepth + 1;
stretch(stretchDepth);
Tree longLivedTree = Tree.with(maxDepth);
for (int depth = minDepth; depth <=maxDepth; depth += 2){
int iterations = 1 << (maxDepth - depth + minDepth);
int sum = 0;
for (int i = 1; i <= iterations; i++){
sum += Count(depth);
}
Console.WriteLine("{0}\t trees of depth {1}\t check: {2}",
iterations, depth, sum);
}
int count = longLivedTree.nodeCount();
longLivedTree.clear();
Console.WriteLine("long lived tree of depth {0}\t check: {1}",
maxDepth, count);
}
static void stretch(int depth){
Console.WriteLine("stretch tree of depth {0}\t check: {1}",
depth, Count(depth));
}
static int Count(int depth){
var t = Tree.with(depth);
int c = t.nodeCount();
t.clear();
return c;
}
}
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
.NET SDK 8.0.301
Host Version: 8.0.6
Commit: 3b8b000a0e
<OutputType>Exe
<TargetFramework>net8.0
<ImplicitUsings>enable
<Nullable>enable
<AllowUnsafeBlocks>true
<ServerGarbageCollection>true
<ConcurrentGarbageCollection>true
<PublishAot>false
Mon, 14 Oct 2024 01:51:56 GMT
MAKE:
cp binarytrees.csharpcore-8.csharpcore Program.cs
cp Include/csharpcore/program.csproj .
mkdir obj
cp Include/csharpcore/project.assets.json ./obj
~/dotnet/dotnet build -c Release --use-current-runtime
Determining projects to restore...
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/program.csproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
Restored /home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/program.csproj (in 6.49 sec).
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/program.csproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
program -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/bin/Release/net8.0/linux-x64/program.dll
Build succeeded.
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/program.csproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
/home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/program.csproj : warning NU1900: Error occurred while getting package vulnerability data: Unable to load the service index for source https://api.nuget.org/v3/index.json.
2 Warning(s)
0 Error(s)
Time Elapsed 00:00:15.54
18.75s to complete and log all make actions
COMMAND LINE:
./bin/Release/net8.0/linux-x64/program 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