binary-trees F# .NET Core #4 program
source code
// The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org/
//
// contributed by Reed Adams
// *reset*
open System
type Node =
| Branch of Node * Node * int
| Leaf of int
let rec GenNode depth item =
if depth = 0
then Leaf(item)
else
let d = depth - 1
Branch( (GenNode d item), (GenNode d item), item )
let inline GenTree depth = GenNode depth
let rec GetCheckSum = function
| Leaf i -> 1
| Branch(l, r, i) -> 1 + (GetCheckSum l) + (GetCheckSum r)
[<EntryPoint>]
let main argv =
let depth = if argv.Length > 0
then Int32.Parse(argv.[0])
else 20
let min_depth = 4
let max_depth = Math.Max(min_depth + 2, depth)
let stretch_depth = max_depth + 1
GenTree stretch_depth 0
|> GetCheckSum
|> printfn "stretch tree of depth %i\t check: %i" stretch_depth
let long_lived_tree = GenTree max_depth 0
for cur_depth in min_depth ..2 ..max_depth do
let iterations = 1 <<< (max_depth - cur_depth + min_depth)
let mutable check = 0
for i in 1 ..iterations do
check <- check
+ GetCheckSum (GenTree cur_depth i)
printfn "%i\t trees of depth %i\t check: %i" iterations cur_depth check
long_lived_tree
|> GetCheckSum
|> printfn "long lived tree of depth %i\t check: %i" max_depth
0
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
2.0.2 a04b4bf512
"System.GC.Server": true
Sat, 11 Nov 2017 21:09:10 GMT
MAKE:
cp binarytrees.fsharpcore-4.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:09.53
10.08s 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