-
Notifications
You must be signed in to change notification settings - Fork 0
/
trees-orig.fs
40 lines (32 loc) · 1.56 KB
/
trees-orig.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Old
type Next = Next of Tree * Tree
and [<Struct>] Tree = Tree of Next
let exec (args: string[]) =
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 |> ignore //stdout.WriteLine
loopTrees |> Array.iter ignore //stdout.WriteLine
longLivedTree.Result |> fst |> ignore //stdout.WriteLine