$ make benchmark /usr/local/bin/ruby -I. ./benchmark/run.rb ruby 1.9.0 powerpc-darwin7.7.0(2005-01-12) YARVCore 0.1.0 rev: 120 (2005-01-09) [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 44.840000 0.280000 45.120000 ( 68.919218) yarv 12.630000 0.040000 12.670000 ( 17.630179) ----------------------------------------------------------- block: def m yield end i=0 while i<10000000 i+=1 m{ } end -- user system total real ruby 32.160000 0.140000 32.300000 ( 48.381697) yarv 4.210000 0.010000 4.220000 ( 5.420375) ----------------------------------------------------------- const: Const = 1 i = 0 while i < 10000000 i+= 1 j = Const end -- user system total real ruby 19.650000 0.070000 19.720000 ( 28.405138) yarv 1.360000 0.000000 1.360000 ( 1.961593) ----------------------------------------------------------- ensure: i=0 while i<1000000 i+=1 begin begin ensure end ensure end end -- user system total real ruby 1.880000 0.010000 1.890000 ( 3.412030) yarv 0.130000 0.000000 0.130000 ( 0.347227) ----------------------------------------------------------- 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.270000 0.010000 0.280000 ( 0.425702) ----------------------------------------------------------- fib: def fib n if n < 3 1 else fib(n-1) + fib(n-2) end end fib(32) -- user system total real ruby 11.120000 0.080000 11.200000 ( 17.845736) yarv 1.150000 0.010000 1.160000 ( 1.578420) ----------------------------------------------------------- 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.010000 0.000000 0.010000 ( 0.007360) ----------------------------------------------------------- regexp: i=0 while i<1000000 /hoge/ =~ 'xxxhogexxx' i+=1 end -- user system total real ruby 3.140000 0.020000 3.160000 ( 4.381932) yarv 1.070000 0.010000 1.080000 ( 1.838452) ----------------------------------------------------------- rescue: i=0 while i<10000000 i+=1 begin rescue end end -- user system total real ruby 20.280000 0.100000 20.380000 ( 29.068469) yarv 1.260000 0.000000 1.260000 ( 2.214611) ----------------------------------------------------------- rescue2: i=0 while i<100000 i+=1 begin raise rescue end end -- user system total real ruby 2.430000 0.170000 2.600000 ( 3.722568) yarv 1.730000 0.230000 1.960000 ( 2.578591) ----------------------------------------------------------- simpleiter: 1000000.times{|simpleiter| simpleiter } -- user system total real ruby 1.010000 0.000000 1.010000 ( 1.512176) yarv 0.410000 0.010000 0.420000 ( 0.550698) ----------------------------------------------------------- simplereturn: def m return 1 end i=0 while i<1000000 i+=1 m end -- user system total real ruby 2.470000 0.000000 2.470000 ( 3.214622) yarv 0.280000 0.010000 0.290000 ( 0.357005) ----------------------------------------------------------- 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.350000 0.000000 0.350000 ( 0.407563) ----------------------------------------------------------- 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 27.110000 0.160000 27.270000 ( 43.220105) yarv 11.180000 0.030000 11.210000 ( 15.219976) ----------------------------------------------------------- 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.830000 0.000000 0.830000 ( 1.064509) yarv 0.290000 0.020000 0.310000 ( 0.446632) ----------------------------------------------------------- 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 0.630000 0.250000 0.880000 ( 1.294465) yarv super: no superclass method `new' ----------------------------------------------------------- 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 11.660000 0.590000 12.250000 ( 18.411746) yarv 7.610000 0.650000 8.260000 ( 11.641515) ----------------------------------------------------------- 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 8.120000 0.080000 8.200000 ( 11.450468) yarv 2.700000 0.020000 2.720000 ( 3.793247) ----------------------------------------------------------- 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 23.980000 0.190000 24.170000 ( 36.906753) yarv 8.410000 0.060000 8.470000 ( 14.221862) ----------------------------------------------------------- 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 20.350000 0.120000 20.470000 ( 30.491635) yarv 16.530000 0.120000 16.650000 ( 26.507122) ----------------------------------------------------------- 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