Fix for JRUBY-2882. Handle error messages related to constructors better
[jruby.git] / bench / shootout / binarytrees.ruby-2.ruby
blobdae85b3b981ebc8bc135810f4ccfc3c631c19aff
1 # The Computer Language Shootout Benchmarks
2 # http://shootout.alioth.debian.org
4 # contributed by Jesse Millikan
6 def item_check(tree)
7 if tree[0] == nil
8 tree[1]
9 else
10 tree[1] + item_check(tree[0]) - item_check(tree[2])
11 end
12 end
14 def bottom_up_tree(item, depth)
15 if depth > 0
16 item_item = 2 * item
17 depth -= 1
18 [bottom_up_tree(item_item - 1, depth), item, bottom_up_tree(item_item, depth)]
19 else
20 [nil, item, nil]
21 end
22 end
24 max_depth = ARGV[0].to_i
25 min_depth = 4
27 max_depth = min_depth + 2 if min_depth + 2 > max_depth
29 stretch_depth = max_depth + 1
30 stretch_tree = bottom_up_tree(0, stretch_depth)
32 puts "stretch tree of depth #{stretch_depth}\t check: #{item_check(stretch_tree)}"
33 stretch_tree = nil
35 long_lived_tree = bottom_up_tree(0, max_depth)
37 min_depth.step(max_depth + 1, 2) do |depth|
38 iterations = 2**(max_depth - depth + min_depth)
40 check = 0
42 for i in 1..iterations
43 temp_tree = bottom_up_tree(i, depth)
44 check += item_check(temp_tree)
46 temp_tree = bottom_up_tree(-i, depth)
47 check += item_check(temp_tree)
48 end
50 puts "#{iterations * 2}\t trees of depth #{depth}\t check: #{check}"
51 end
53 puts "long lived tree of depth #{max_depth}\t check: #{item_check(long_lived_tree)}"