The Computer Language
24.11 Benchmarks Game

binary-trees Ruby yjit #8 program

source code

# The Computer Language Benchmarks Game
# https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
#
# contributed by Isaac Gouy

class Tree
  attr_accessor :left, :right

  def initialize(left, right)
    @left = left
    @right = right 
  end     
  
  def self.with depth
    depth == 0 \
      ? self.new(nil, nil) \
      : self.new( Tree.with(depth-1), Tree.with(depth-1) ) 
  end    
  
  def node_count
    @left == nil \
      ? 1 \
      : 1 + @left.node_count + @right.node_count
  end     
  
  def clear
    if @left != nil
      @left.clear
      @left = nil
      @right.clear      
      @right = nil         
    end  
  end   
end

def stretch(depth)           
  puts "stretch tree of depth #{depth}\t check: #{count(depth)}"   
end

def count(depth)
  t = Tree.with(depth)        
  c = t.node_count()
  t.clear() 
  return c
end

def main n
  min_depth = 4  
  max_depth = min_depth + 2 > n ? min_depth + 2 : n
  stretch_depth = max_depth + 1  
  
  stretch(stretch_depth)  
  long_lived_tree = Tree.with max_depth   
  
  for depth in (min_depth .. max_depth).step(2)
    iterations = 1 << (max_depth - depth + min_depth)  
    sum = 0      
    for i in 1 .. iterations 
      sum += count(depth)
    end      
    puts "#{iterations}\t trees of depth #{depth}\t check: #{sum}"     
  end
  c = long_lived_tree.node_count  
  long_lived_tree.clear()  
  puts "long lived tree of depth #{max_depth}\t check: #{c}"  
end 

n = ARGV.empty? ? 10 : ARGV[0].to_i
main n

    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
ruby 3.3.5
(2024-09-03
revision ef084cc8f4)
+YJIT [x86_64-linux]


 Mon, 14 Oct 2024 02:04:52 GMT

COMMAND LINE:
 /opt/src/ruby-3.3.5/bin/ruby --yjit -W0 binarytrees.ruby-8.ruby 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