Skip to content

Instantly share code, notes, and snippets.

@maleadt
Created November 16, 2018 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maleadt/82d88557f6874e3a43cfbe7892ff70eb to your computer and use it in GitHub Desktop.
Save maleadt/82d88557f6874e3a43cfbe7892ff70eb to your computer and use it in GitHub Desktop.
# The Computer Language Benchmarks Game
# binary-trees benchmark
# http://shootout.alioth.debian.org/u32/performance.php?test=binarytrees
#
# Ported from an OCaml version
abstract type BTree end
mutable struct Empty <: BTree
end
mutable struct Node <: BTree
info
left::BTree
right::BTree
end
function make(val, d)
if d == 0
Node(val, Empty(), Empty())
else
nval = val * 2
Node(val, make(nval-1, d-1), make(nval, d-1))
end
end
check(t::Empty) = 0
check(t::Node) = t.info + check(t.left) - check(t.right)
function loop_depths(d, min_depth, max_depth)
for i = 0:div(max_depth - d, 2)
niter = 1 << (max_depth - d + min_depth)
c = 0
for j = 1:niter
c += check(make(i, d)) + check(make(-i, d))
end,# @printf("%i\t trees of depth %i\t check: %i\n", 2*niter, d, c)
d += 2
end
end
const N=10
min_depth = 4
max_depth = N
stretch_depth = max_depth + 1
# create and check stretch tree
let c = check(make(0, stretch_depth))
# @printf("stretch tree of depth %i\t check: %i\n", stretch_depth, c)
end
long_lived_tree = make(0, max_depth)
loop_depths(min_depth, min_depth, max_depth)
# @printf("long lived tree of depth %i\t check: %i\n", max_depth, check(long_lived_tree))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment