The Computer Language
Benchmarks Game

binary-trees Ruby JRuby #3 program

source code

# The Computer Language Benchmarks Game
# http://benchmarksgame.alioth.debian.org
#
# contributed by Jesse Millikan
# Modified by Wesley Moxam and Michael Klaus
# Modified by  Chris Houhoulis:
#   trigger GC; move stretch_tree into loop; use while instead of a block
#  *reset* 

def item_check(left, right)
  return 1 if left.nil?
  1 + item_check(*left) + item_check(*right)
end

def bottom_up_tree(depth)
  return [nil, nil] if depth == 0
  depth -= 1
  [bottom_up_tree(depth), bottom_up_tree(depth)]
end

max_depth = ARGV[0].to_i
min_depth = 4

max_depth = [min_depth + 2, max_depth].max

1.times do
  stretch_depth = max_depth + 1
  stretch_tree = bottom_up_tree(stretch_depth)
  puts "stretch tree of depth #{stretch_depth}\t check: " +
    "#{item_check(*stretch_tree)}"
end
GC.start

long_lived_tree = bottom_up_tree(max_depth)

base_depth = max_depth + min_depth
min_depth.step(max_depth, 2) do |depth|
  iterations = 2 ** (base_depth - depth)

  check, i = 0, 1

  while i <= iterations
    temp_tree = bottom_up_tree(depth)
    check += item_check(*temp_tree)
    i += 1
  end

  puts "#{iterations}\t trees of depth #{depth}\t check: #{check}"
end
GC.start

puts "long lived tree of depth #{max_depth}\t check: " +
  "#{item_check(*long_lived_tree)}"
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
jruby 9.1.13.0 (2.3.3) 2017-09-06 8e1c115 Java HotSpot(TM) 64-Bit Server VM 9+181 on 9+181 +jit [linux-x86_64]


Thu, 21 Sep 2017 21:48:28 GMT

MAKE:
mv binarytrees.jruby-3.jruby binarytrees.rb

0.05s to complete and log all make actions

COMMAND LINE:
/usr/local/src/jruby-9.1.13.0/bin/jruby -Xcompile.fastest=true -Xcompile.invokedynamic=true -J-server -J-Xmx2G binarytrees.rb 21

PROGRAM OUTPUT:
stretch tree of depth 22	 check: 8388607
2097152	 trees of depth 4	 check: 65011712
524288	 trees of depth 6	 check: 66584576
131072	 trees of depth 8	 check: 66977792
32768	 trees of depth 10	 check: 67076096
8192	 trees of depth 12	 check: 67100672
2048	 trees of depth 14	 check: 67106816
512	 trees of depth 16	 check: 67108352
128	 trees of depth 18	 check: 67108736
32	 trees of depth 20	 check: 67108832
long lived tree of depth 21	 check: 4194303

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.jruby.util.io.FilenoUtil to method sun.nio.ch.SelChImpl.getFD()
WARNING: Please consider reporting this to the maintainers of org.jruby.util.io.FilenoUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release