The Computer Language
24.04 Benchmarks Game

binary-trees F# .NET program

source code

(* The Computer Language Benchmarks Game
   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

   Contributed by Don Syme
   Port of C# version by by Marek Safar and optimized by kasthack
   *reset*
*)

open System

[<AllowNullLiteral>]
type TreeNode(left:TreeNode,right:TreeNode) = 
    member __.CheckSum =
        match right with 
        | null -> 1 
        | _ -> 1 + left.CheckSum + right.CheckSum

let rec mkTree(depth) =
    if depth = 0 then TreeNode(null, null)
    else TreeNode(mkTree (depth-1), mkTree(depth-1))

let bottomUpTree (depth) = mkTree(depth)

let minDepth = 4
[<EntryPoint>]
let main argv = 
    let n = if argv.Length > 0 then Int32.Parse(argv.[0]) else 0
    let maxDepth = Math.Max(minDepth + 2, n)
    let stretchDepth = maxDepth + 1
    let mutable check = bottomUpTree(stretchDepth).CheckSum
    Console.WriteLine("stretch tree of depth {0}\t check: {1}", stretchDepth, check)
    let longLivedTree = bottomUpTree(maxDepth)
    for depth in minDepth .. 2 .. maxDepth do
         let iterations = 1 <<< ( maxDepth - depth + minDepth )
         check <- 0
         for i in 1 .. iterations do 
            check <- check + bottomUpTree(depth).CheckSum
         Console.WriteLine("{0}\t trees of depth {1}\t check: {2}",iterations, depth, check)
    Console.WriteLine("long lived tree of depth {0}\t check: {1}",maxDepth, longLivedTree.CheckSum)
    0

    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
.NET SDK 8.0.200
Host Version: 8.0.2
Commit: 1381d5ebd2
<ServerGarbageCollection>true
F# 8.0



 Mon, 01 Apr 2024 23:05:49 GMT

MAKE:
cp binarytrees.fsharpcore Program.fs
cp Include/fsharpcore/tmp.fsproj .
mkdir obj
cp Include/fsharpcore/project.assets.json ./obj
/usr/bin/dotnet build -c Release --no-self-contained -r linux-x64 	
MSBuild version 17.9.6+a4ecab324 for .NET
  Determining projects to restore...
  Restored /home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/tmp.fsproj (in 1.79 sec).
  tmp -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/bin/Release/net8.0/linux-x64/tmp.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:09.46

11.44s to complete and log all make actions

COMMAND LINE:
 ./bin/Release/net8.0/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