The Computer Language
22.05 Benchmarks Game

binary-trees F# .NET #7 program

source code

// The Computer Language Benchmarks Game
// https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
//
// Based on Java version by Jarkko Miettinen's
// Contributed by Vasily Kirichenko

[<Sealed; AllowNullLiteral>]
type TreeNode(left: TreeNode, right: TreeNode) =
    member this.itemCheck() = 
        if isNull left then 1 else 1 + left.itemCheck() + right.itemCheck()


let rec bottomUpTree(depth: int) : TreeNode =
    if depth > 0 then
        TreeNode(bottomUpTree(depth - 1), bottomUpTree(depth - 1))
    else
        TreeNode(null, null)

[<EntryPoint>]
let main args =
    let n = match args with [|n|] -> int n | _ -> 0
    let minDepth = 4
    let maxDepth = max (minDepth + 2) n
    let stretchDepth = maxDepth + 1

    printfn "stretch tree of depth %d\t check: %d" stretchDepth (bottomUpTree(stretchDepth).itemCheck())

    let longLivedTree = bottomUpTree maxDepth

    [| for depth in minDepth..2..maxDepth do
         yield async {
            let iterations = 1 <<< (maxDepth - depth + minDepth)
            let check = Array.init iterations (fun _ -> bottomUpTree(depth).itemCheck()) |> Array.sum
            return sprintf "%d\t trees of depth %d\t check: %d" iterations depth check
         } |]
    |> Async.Parallel
    |> Async.RunSynchronously
    |> Array.iter (printfn "%s")

    printfn "long lived tree of depth %d\t check: %d" maxDepth (longLivedTree.itemCheck())
    0
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
.NET SDK 6.0.101
Host Version: 6.0.1;
Commit: 3a25a7f1cc
<ServerGarbageCollection>true


Tue, 10 May 2022 03:22:43 GMT

MAKE:
cp binarytrees.fsharpcore-7.fsharpcore Program.fs
cp Include/fsharpcore/tmp.fsproj .
mkdir obj
cp Include/fsharpcore/project.assets.json ./obj
/usr/bin/dotnet build -c Release --no-restore --no-self-contained -r ubuntu-x64 
Microsoft (R) Build Engine version 17.1.1+a02f73656 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  tmp -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/binarytrees/tmp/bin/Release/net6.0/ubuntu-x64/tmp.dll

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

Time Elapsed 00:00:07.41

8.90s to complete and log all make actions

COMMAND LINE:
/usr/bin/dotnet ./bin/Release/net6.0/ubuntu-x64/tmp.dll 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