ruby 1.9.0 alphaev6-osf5.1b(2005-03-03) YARVCore 0.2.0 rev: 154 (2005-03-04) [direct threaded code] [optimize basic operation] [optimize regexp match] [stack caching] [inline method cache] ----------------------------------------------------------- array: i=0 while i<10000000 i+=1 a = [1,2,3,4,5,6,7,8,9,10] end -- user system total real ruby 47.566667 0.016667 47.583333 ( 47.604647) yarv 15.850000 0.000000 15.850000 ( 15.879608) ----------------------------------------------------------- block: def m yield end i=0 while i<1000000 i+=1 m{ } end -- user system total real ruby 3.450000 0.000000 3.450000 ( 3.450136) yarv 1.283333 0.000000 1.283333 ( 1.282205) ----------------------------------------------------------- const: Const = 1 i = 0 while i < 10000000 i+= 1 j = Const k = Const end -- user system total real ruby 28.516667 0.000000 28.516667 ( 28.533688) yarv 8.583333 0.000000 8.583333 ( 8.592626) ----------------------------------------------------------- ensure: i=0 while i<1000000 i+=1 begin begin ensure end ensure end end -- user system total real ruby 1.983333 0.000000 1.983333 ( 1.994106) yarv 0.700000 0.000000 0.700000 ( 0.694324) ----------------------------------------------------------- factorial: def fact(n) if(n > 1) n * fact(n-1) else 1 end end fact(7300) -- user system total real ruby stack level too deep yarv 0.316667 0.050000 0.366667 ( 0.372064) ----------------------------------------------------------- fib: def fib n if n < 3 1 else fib(n-1) + fib(n-2) end end fib(32) -- user system total real ruby 12.766667 0.000000 12.766667 ( 12.770287) yarv 3.733333 0.000000 3.733333 ( 3.744076) ----------------------------------------------------------- lists: #from http://www.bagley.org/~doug/shootout/bench/lists/lists.ruby NUM = 10 SIZE = 10000 def test_lists() # create a list of integers (Li1) from 1 to SIZE li1 = (1..SIZE).to_a # copy the list to li2 (not by individual items) li2 = li1.dup # remove each individual item from left side of li2 and # append to right side of li3 (preserving order) li3 = Array.new while (not li2.empty?) li3.push(li2.shift) end # li2 must now be empty # remove each individual item from right side of li3 and # append to right side of li2 (reversing list) while (not li3.empty?) li2.push(li3.pop) end # li3 must now be empty # reverse li1 in place li1.reverse! # check that first item is now SIZE if li1[0] != SIZE then p "not SIZE" 0 else # compare li1 and li2 for equality if li1 != li2 then return(0) else # return the length of the list li1.length end end end i = 0 while i 1 1 + reccount(n-1) else 1 end end reccount 7000 -- user system total real ruby stack level too deep yarv 0.000000 0.016667 0.016667 ( 0.012695) ----------------------------------------------------------- regexp: i=0 while i<1000000 /hoge/ =~ 'xxxhogexxx' i+=1 end -- user system total real ruby 4.250000 0.000000 4.250000 ( 4.258715) yarv 2.900000 0.000000 2.900000 ( 2.905224) ----------------------------------------------------------- rescue: i=0 while i<10000000 i+=1 begin rescue end end -- user system total real ruby 21.683333 0.000000 21.683333 ( 21.702751) yarv 9.366667 0.000000 9.366667 ( 9.373862) ----------------------------------------------------------- rescue2: i=0 while i<100000 i+=1 begin raise rescue end end -- user system total real ruby 3.416667 0.116667 3.533333 ( 3.544861) yarv 3.033333 0.150000 3.183333 ( 3.171821) ----------------------------------------------------------- simpleiter: 1000000.times{|simpleiter| simpleiter } -- user system total real ruby 0.916667 0.000000 0.916667 ( 0.916000) yarv 0.383333 0.000000 0.383333 ( 0.386712) ----------------------------------------------------------- simplereturn: def m return 1 end i=0 while i<1000000 i+=1 m end -- user system total real ruby 2.700000 0.000000 2.700000 ( 2.700148) yarv 1.133333 0.000000 1.133333 ( 1.126934) ----------------------------------------------------------- so_ackermann: #!/usr/bin/ruby # -*- mode: ruby -*- # $Id: ackermann-ruby.code,v 1.4 2004/11/13 07:40:41 bfulgham Exp $ # http://www.bagley.org/~doug/shootout/ def ack(m, n) if m == 0 then n + 1 elsif n == 0 then ack(m - 1, 1) else ack(m - 1, ack(m, n - 1)) end end NUM = 7 ack(3, NUM) -- user system total real ruby stack level too deep yarv 0.883333 0.000000 0.883333 ( 0.879868) ----------------------------------------------------------- so_array: #!/usr/bin/ruby # -*- mode: ruby -*- # $Id: ary-ruby.code,v 1.4 2004/11/13 07:41:27 bfulgham Exp $ # http://www.bagley.org/~doug/shootout/ # with help from Paul Brannan and Mark Hubbart n = 9000 # Integer(ARGV.shift || 1) x = Array.new(n) y = Array.new(n, 0) n.times{|bi| x[bi] = bi + 1 } (0 .. 999).each do |e| (n-1).step(0,-1) do |bi| y[bi] += x.at(bi) end end # puts "#{y.first} #{y.last}" -- user system total real ruby 31.150000 0.000000 31.150000 ( 31.169384) yarv 18.400000 0.000000 18.400000 ( 18.407886) ----------------------------------------------------------- so_concatenate: #!/usr/bin/ruby # -*- mode: ruby -*- # $Id: strcat-ruby.code,v 1.4 2004/11/13 07:43:28 bfulgham Exp $ # http://www.bagley.org/~doug/shootout/ # based on code from Aristarkh A Zagorodnikov and Dat Nguyen STUFF = "hello\n" hello = '' 400000.times do |e| hello << STUFF end # puts hello.length -- user system total real ruby 0.866667 0.033333 0.900000 ( 0.890609) yarv 0.366667 0.016667 0.383333 ( 0.391595) ----------------------------------------------------------- so_count_words: #!/usr/bin/ruby # -*- mode: ruby -*- # $Id: wc-ruby.code,v 1.4 2004/11/13 07:43:32 bfulgham Exp $ # http://www.bagley.org/~doug/shootout/ # with help from Paul Brannan require 'stringio' input = StringIO.new data = File.read($DRIVER_PATH + '/wc.input') 5000.times{|i| input.write data } input.seek 0 nl = nw = nc = 0 while true data = (input.read(4096) or break) << (input.gets || "") nc += data.length nl += data.count("\n") ((data.strip! || data).tr!("\n", " ") || data).squeeze! nw += data.count(" ") + 1 end # puts "#{nl} #{nw} #{nc}" -- user system total real ruby 1.333333 0.233333 1.566667 ( 1.566379) yarv 1.283333 0.233333 1.516667 ( 1.517552) ----------------------------------------------------------- so_exception: #!/usr/bin/ruby # -*- mode: ruby -*- # $Id: except-ruby.code,v 1.4 2004/11/13 07:41:33 bfulgham Exp $ # http://www.bagley.org/~doug/shootout/ $HI = 0 $LO = 0 NUM = 250000 # Integer(ARGV[0] || 1) class Lo_Exception < Exception def initialize(num) @value = num end end class Hi_Exception < Exception def initialize(num) @value = num end end def some_function(num) begin hi_function(num) rescue print "We shouldn't get here, exception is: #{$!.type}\n" end end def hi_function(num) begin lo_function(num) rescue Hi_Exception $HI = $HI + 1 end end def lo_function(num) begin blowup(num) rescue Lo_Exception $LO = $LO + 1 end end def blowup(num) if num % 2 == 0 raise Lo_Exception.new(num) else raise Hi_Exception.new(num) end end i = 1 max = NUM+1 while i < max i+=1 some_function(i+1) end -- user system total real ruby 15.550000 0.550000 16.100000 ( 16.107144) yarv 13.000000 0.416667 13.416667 ( 13.426526) ----------------------------------------------------------- so_matrix: #!/usr/bin/ruby # -*- mode: ruby -*- # $Id: matrix-ruby.code,v 1.4 2004/11/13 07:42:14 bfulgham Exp $ # http://www.bagley.org/~doug/shootout/ n = 60 #Integer(ARGV.shift || 1) size = 30 def mkmatrix(rows, cols) count = 1 mx = Array.new(rows) (0 .. (rows - 1)).each do |bi| row = Array.new(cols, 0) (0 .. (cols - 1)).each do |j| row[j] = count count += 1 end mx[bi] = row end mx end def mmult(rows, cols, m1, m2) m3 = Array.new(rows) (0 .. (rows - 1)).each do |bi| row = Array.new(cols, 0) (0 .. (cols - 1)).each do |j| val = 0 (0 .. (cols - 1)).each do |k| val += m1.at(bi).at(k) * m2.at(k).at(j) end row[j] = val end m3[bi] = row end m3 end m1 = mkmatrix(size, size) m2 = mkmatrix(size, size) mm = Array.new n.times do mm = mmult(size, size, m1, m2) end # puts "#{mm[0][0]} #{mm[2][3]} #{mm[3][2]} #{mm[4][4]}" -- user system total real ruby 9.500000 0.033333 9.533333 ( 9.529133) yarv 4.400000 0.033333 4.433333 ( 4.424728) ----------------------------------------------------------- so_nested_loop: #!/usr/bin/ruby # -*- mode: ruby -*- # $Id: nestedloop-ruby.code,v 1.4 2004/11/13 07:42:22 bfulgham Exp $ # http://www.bagley.org/~doug/shootout/ # from Avi Bryant n = 16 # Integer(ARGV.shift || 1) x = 0 n.times do n.times do n.times do n.times do n.times do n.times do x += 1 end end end end end end # puts x -- user system total real ruby 26.266667 0.000000 26.266667 ( 26.277867) yarv 11.316667 0.016667 11.333333 ( 11.337695) ----------------------------------------------------------- so_object: #!/usr/bin/ruby # -*- mode: ruby -*- # $Id: objinst-ruby.code,v 1.4 2004/11/13 07:42:25 bfulgham Exp $ # http://www.bagley.org/~doug/shootout/ # with help from Aristarkh Zagorodnikov class Toggle def initialize(start_state) @bool = start_state end def value @bool end def activate @bool = !@bool self end end class NthToggle < Toggle def initialize(start_state, max_counter) super start_state @count_max = max_counter @counter = 0 end def activate @counter += 1 if @counter >= @count_max @bool = !@bool @counter = 0 end self end end n = 1500000 # (ARGV.shift || 1).to_i toggle = Toggle.new 1 5.times do toggle.activate.value ? 'true' : 'false' end n.times do toggle = Toggle.new 1 end ntoggle = NthToggle.new 1, 3 8.times do ntoggle.activate.value ? 'true' : 'false' end n.times do ntoggle = NthToggle.new 1, 3 end -- user system total real ruby 22.783333 0.016667 22.800000 ( 22.829684) yarv 23.300000 0.033333 23.333333 ( 23.340418) ----------------------------------------------------------- so_random: # from http://www.bagley.org/~doug/shootout/bench/random/random.ruby IM = 139968 IA = 3877 IC = 29573 $last = 42.0 def gen_random(max) (max * ($last = ($last * IA + IC) % IM)) / IM end N = 1000000 i=0 while i