github.com/coyove/nj@v0.0.0-20221110084952-c7f8db1065c3/tests/bench/binarytree.tengo (about) 1 n := 23 2 3 bottomUpTree := func(depth) { 4 if (depth <= 0) { 5 return {} 6 } 7 return { "left": bottomUpTree(depth-1), "right": bottomUpTree(depth-1) } 8 } 9 10 itemCheck := func(n) { 11 if (len(n) == 0) { 12 return 1 13 } 14 return itemCheck(n.left) + itemCheck(n.right) + 1 15 } 16 17 minDepth := 4 18 maxDepth := n 19 if (minDepth + 2 > n) { 20 maxDepth = minDepth + 2 21 } 22 23 stretchDepth := maxDepth + 1 24 25 check_l := itemCheck(bottomUpTree(stretchDepth)) 26 fmt := import("fmt") 27 fmt.println("stretch tree of depth ", stretchDepth, " check:", check_l) 28 29 //longLivedTree := bottomUpTree(maxDepth) 30 //result = std.genlist(maxDepth+1) 31 // 32 //fun worker( depth, check) { 33 // iterations = 1 << (maxDepth - depth + minDepth) 34 // check = 0 35 // 36 // for i = 0,iterations { 37 // check = check + itemCheck(bottomUpTree(depth)) 38 // } 39 // result[depth] = std.sprintf("{}\t trees of depth %s\t check: {}", iterations, depth, check) 40 // wg.done() 41 //} 42 // 43 //for depth_l = minDepth,2,maxDepth { 44 // wg.add(1) 45 // std.sync.run(worker, depth_l, check_l) 46 //} 47 // 48 //wg.wait() 49 // 50 //for depth = minDepth,2,maxDepth { 51 // io.println( result[depth]) 52 //} 53 //io.printf("long lived tree of depth {}\t check: {}\n", maxDepth, itemCheck(longLivedTree))