/usr/local/bin/ruby_cyg19 -I.. ../benchmark/run.rb ruby 1.9.0 i386-cygwin(2005-01-09) 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 27.829000 0.000000 27.829000 ( 27.904000) yarv 16.688000 0.015000 16.703000 ( 16.718000) ----------------------------------------------------------- block: def m yield end i=0 while i<10000000 i+=1 m{ } end -- user system total real ruby 14.843000 0.000000 14.843000 ( 14.850000) yarv 2.609000 0.000000 2.609000 ( 2.585000) ----------------------------------------------------------- const: Const = 1 i = 0 while i < 10000000 i+= 1 j = Const end -- user system total real ruby 9.125000 0.016000 9.141000 ( 9.120000) yarv 0.719000 0.000000 0.719000 ( 0.714000) ----------------------------------------------------------- ensure: i=0 while i<1000000 i+=1 begin begin ensure end ensure end end -- user system total real ruby 0.797000 0.000000 0.797000 ( 0.789000) yarv 0.078000 0.015000 0.093000 ( 0.075000) ----------------------------------------------------------- factorial: def fact(n) if(n > 1) n * fact(n-1) else 1 end end fact(7300) -- user system total real ruby 0.719000 0.079000 0.798000 ( 0.797000) yarv 0.516000 0.032000 0.548000 ( 0.541000) ----------------------------------------------------------- fib: def fib n if n < 3 1 else fib(n-1) + fib(n-2) end end fib(32) -- user system total real ruby 5.640000 0.000000 5.640000 ( 5.634000) yarv 0.735000 0.000000 0.735000 ( 0.730000) ----------------------------------------------------------- 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 0.079000 0.094000 0.173000 ( 0.168000) yarv 0.000000 0.015000 0.015000 ( 0.000000) ----------------------------------------------------------- regexp: i=0 while i<1000000 /hoge/ =~ 'xxxhogexxx' i+=1 end -- user system total real ruby 2.188000 0.000000 2.188000 ( 2.208000) yarv 1.407000 0.000000 1.407000 ( 1.400000) ----------------------------------------------------------- rescue: i=0 while i<10000000 i+=1 begin rescue end end -- user system total real ruby 8.594000 0.000000 8.594000 ( 8.617000) yarv 0.672000 0.000000 0.672000 ( 0.663000) ----------------------------------------------------------- rescue2: i=0 while i<100000 i+=1 begin raise rescue end end -- user system total real ruby 3.110000 0.000000 3.110000 ( 3.124000) yarv 2.234000 0.000000 2.234000 ( 2.232000) ----------------------------------------------------------- simpleiter: 1000000.times{|simpleiter| simpleiter } -- user system total real ruby 0.484000 0.016000 0.500000 ( 0.491000) yarv 0.188000 0.000000 0.188000 ( 0.197000) ----------------------------------------------------------- simplereturn: def m return 1 end i=0 while i<1000000 i+=1 m end -- user system total real ruby 1.110000 0.000000 1.110000 ( 1.123000) yarv 0.172000 0.000000 0.172000 ( 0.162000) ----------------------------------------------------------- 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 4.110000 0.015000 4.125000 ( 4.104000) yarv 0.250000 0.000000 0.250000 ( 0.244000) ----------------------------------------------------------- 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 14.625000 0.000000 14.625000 ( 14.628000) yarv 7.516000 0.000000 7.516000 ( 7.523000) ----------------------------------------------------------- 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.437000 0.031000 0.468000 ( 0.466000) yarv 0.187000 0.000000 0.187000 ( 0.187000) ----------------------------------------------------------- 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.125000 0.140000 1.265000 ( 1.323000) 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 13.500000 0.016000 13.516000 ( 13.540000) yarv 10.562000 0.000000 10.562000 ( 10.630000) ----------------------------------------------------------- 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 4.343000 0.016000 4.359000 ( 4.344000) yarv 2.032000 0.000000 2.032000 ( 2.052000) ----------------------------------------------------------- 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 11.609000 0.016000 11.625000 ( 11.620000) yarv 3.734000 0.000000 3.734000 ( 3.708000) ----------------------------------------------------------- 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 21.422000 0.000000 21.422000 ( 21.417000) yarv 19.844000 0.016000 19.860000 ( 19.849000) ----------------------------------------------------------- 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