binary-trees F# .NET Core #8 program
source code
// The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org/
//
// Modification by Don Syme & Jomo Fisher to use a nullable Next element
// Based on F# version by Robert Pickering
// Based on ocaml version by Troestler Christophe & Isaac Gouy
// Multithreaded by Anthony Lloyd
type Next = Next of Tree * Tree
and [<Struct>] Tree = Tree of Next
[<EntryPoint>]
let main args =
let minDepth = 4
let maxDepth =if args.Length=0 then 10 else max (minDepth+2) (int args.[0])
let stretchDepth = maxDepth + 1
let rec make depth =
if depth=0 then Tree Unchecked.defaultof<_>
else Next (make (depth-1), make (depth-1)) |> Tree
let rec check (Tree n) =
if box n |> isNull then 1
else let (Next(l,r)) = n in 1 + check l + check r
let stretchTreeCheck = System.Threading.Tasks.Task.Run(fun () ->
let check = make stretchDepth |> check |> string
"stretch tree of depth "+string stretchDepth+"\t check: "+check )
let longLivedTree = System.Threading.Tasks.Task.Run(fun () ->
let tree = make maxDepth
let check = check tree |> string
"long lived tree of depth "+string maxDepth+"\t check: "+check, tree )
let loopTrees = Array.init ((maxDepth-minDepth)/2+1) (fun d ->
let d = minDepth+d*2
let n = 1 <<< (maxDepth - d + minDepth)
let c = Array.Parallel.init System.Environment.ProcessorCount (fun _ ->
let mutable c = 0
for __ = 1 to n/System.Environment.ProcessorCount do
c <- c + (make d |> check)
c ) |> Array.sum
string n+"\t trees of depth "+string d+"\t check: "+string c )
stretchTreeCheck.Result |> stdout.WriteLine
loopTrees |> Array.iter stdout.WriteLine
longLivedTree.Result |> fst |> stdout.WriteLine
exit 0
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
2.0.2 a04b4bf512
"System.GC.Server": true
Sun, 12 Nov 2017 17:27:47 GMT
MAKE:
cp binarytrees.fsharpcore-8.fsharpcore Program.fs
cp Include/fsharpcore/tmp.fsproj .
cp Include/fsharpcore/runtimeconfig.template.json .
mkdir obj
cp Include/fsharpcore/project.assets.json ./obj
cp Include/fsharpcore/tmp.fsproj.nuget.g.props ./obj
cp Include/fsharpcore/tmp.fsproj.nuget.g.targets ./obj
/usr/bin/dotnet build -c Release --no-restore
Microsoft (R) Build Engine version 15.4.8.50001 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
tmp -> /home/dunham/benchmarksgame_quadcore/binarytrees/tmp/bin/Release/netcoreapp2.0/tmp.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:12.16
12.76s to complete and log all make actions
COMMAND LINE:
/usr/bin/dotnet ./bin/Release/netcoreapp2.0/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