Files
wren/benchmark/binary_trees.wren
Bob Nystrom 5aaaa33552 Revise benchmarks:
- Ditch JS since it's in a different league.
- Make binary_trees and fib run faster.
- Compare using best time instead of mean.
2013-12-12 16:59:57 -08:00

61 lines
1.2 KiB
Plaintext

// Ported from the Python version.
class Tree {
this new(item, depth) {
_item = item
if (depth > 0) {
var item2 = item + item
depth = depth - 1
_left = Tree.new(item2 - 1, depth)
_right = Tree.new(item2, depth)
}
}
check {
if (_left == null) {
return _item
}
return _item + _left.check - _right.check
}
}
var minDepth = 4
var maxDepth = 12
var stretchDepth = maxDepth + 1
var start = OS.clock
io.write("stretch tree of depth " + stretchDepth.toString + " check: " +
Tree.new(0, stretchDepth).check.toString)
var longLivedTree = Tree.new(0, maxDepth)
// iterations = 2 ** maxDepth
var iterations = 1
var d = 0
while (d < maxDepth) {
iterations = iterations * 2
d = d + 1
}
var depth = minDepth
while (depth < stretchDepth) {
var check = 0
var i = 1
while (i < iterations + 1) {
check = check + Tree.new(i, depth).check + Tree.new(-i, depth).check
i = i + 1
}
io.write((iterations * 2).toString + " trees of depth " + depth.toString +
" check: " + check.toString)
iterations = iterations / 4
depth = depth + 2
}
io.write("long lived tree of depth " + maxDepth.toString + " check: " +
longLivedTree.check.toString)
io.write("elapsed: " + (OS.clock - start).toString)