[MacRuby-devel] [MacRuby] #324: improve bench.rb

MacRuby ruby-noreply at macosforge.org
Thu Sep 3 14:26:26 PDT 2009


#324: improve bench.rb
-------------------------------------+--------------------------------------
 Reporter:  jordan.breeding@…        |       Owner:  lsansonetti@…        
     Type:  defect                   |      Status:  new                  
 Priority:  blocker                  |   Milestone:  MacRuby 0.5          
Component:  MacRuby                  |    Keywords:                       
-------------------------------------+--------------------------------------
 Currently bench.rb has a few inconsistencies (number printed to screen is
 not the number of times looped) and doesn't work well under ruby 1.8 or
 1.9.

 Having it work on multiple versons of ruby is useful for benchmarking
 regressions against other ruby versions.

 I am including a patch I use locally to use able to run bench.rb under all
 ruby versions (by catching errors).

 {{{
 diff --git a/bench.rb b/bench.rb
 index e05833b..2c47c62 100644
 --- a/bench.rb
 +++ b/bench.rb
 @@ -7,6 +7,16 @@
  # The same script can also be executed using /usr/bin/ruby or ruby19 to
  # compare the implementations.

 +def executeWithErrorHandling
 +  if (block_given?)
 +    begin
 +      yield
 +    rescue Exception => e
 +      print(" ************* BENCHMARK FAILED ************* ")
 +    end
 +  end
 +end
 +
  def fib(n)
    if n < 3
      1
 @@ -80,168 +90,248 @@ Benchmark.bm(30) do |bm|

    # Fixnum arithmetic.
    bm.report('10 fib(30)') do
 -    i=0; while i<10; fib(30); i+=1; end
 +    executeWithErrorHandling do
 +      i=0; while i<10; fib(30); i+=1; end
 +    end
    end
    bm.report('10 fib(35)') do
 -    i=0; while i<10; fib(35); i+=1; end
 +    executeWithErrorHandling do
 +      i=0; while i<10; fib(35); i+=1; end
 +    end
 +  end
 +  bm.report('tak') do
 +    executeWithErrorHandling do
 +      tak(18,9,0)
 +    end
 +  end
 +  bm.report('tarai') do
 +    executeWithErrorHandling do
 +      tarai(12,6,0)
 +    end
    end
 -  bm.report('tak') { tak(18,9,0) }
 -  bm.report('tarai') { tarai(12,6,0) }
 -  if RUBY_VERSION.to_f > 1.8
 -    # Ruby 1.8 is too weak for this benchmark.
 -    bm.report('ackermann') { ack(3,9) }
 +  bm.report('ackermann') do
 +    executeWithErrorHandling do
 +      ack(3,9)
 +    end
    end

    # Loops.
    bm.report('10000000 times loop') do
 -    3000000.times {}
 +    executeWithErrorHandling do
 +      10000000.times {}
 +    end
    end
    bm.report('30000000 times loop') do
 -    30000000.times {}
 +    executeWithErrorHandling do
 +      30000000.times {}
 +    end
    end
    bm.report('10000000 while loop') do
 -    i=0; while i<10000000; i+=1; end
 +    executeWithErrorHandling do
 +      i=0; while i<10000000; i+=1; end
 +    end
    end
    bm.report('60000000 while loop') do
 -    i=0; while i<60000000; i+=1; end
 +    executeWithErrorHandling do
 +      i=0; while i<60000000; i+=1; end
 +    end
    end

    # Messages.
    bm.report('30000000 msg w/ 0 arg') do
 -    o = Class1.new
 -    i=0; while i<10000000; o.method1; o.method1; o.method1; i+=1; end
 +    executeWithErrorHandling do
 +      o = Class1.new
 +      i=0; while i<30000000; o.method1; o.method1; o.method1; i+=1; end
 +    end
    end
    bm.report('30000000 msg w/ 1 arg') do
 -    o = Class1.new
 -    i=0; while i<10000000; o.method2(i); o.method2(i); o.method2(i);
 i+=1; end
 +    executeWithErrorHandling do
 +      o = Class1.new
 +      i=0; while i<30000000; o.method2(i); o.method2(i); o.method2(i);
 i+=1; end
 +    end
    end
    bm.report('10000000 super w/ 0 arg') do
 -    o = Class2.new
 -    i=0; while i<10000000; o.method1; i+=1; end
 +    executeWithErrorHandling do
 +      o = Class2.new
 +      i=0; while i<10000000; o.method1; i+=1; end
 +    end
    end
    bm.report('10000000 super w/ 1 arg') do
 -    o = Class2.new
 -    i=0; while i<10000000; o.method2(i); i+=1; end
 +    executeWithErrorHandling do
 +      o = Class2.new
 +      i=0; while i<10000000; o.method2(i); i+=1; end
 +    end
    end
    bm.report('10000000 #send') do
 -    o = Class1.new
 -    i=0; while i<10000000; o.send(:method1); i+=1; end
 +    executeWithErrorHandling do
 +      o = Class1.new
 +      i=0; while i<10000000; o.send(:method1); i+=1; end
 +    end
    end
    bm.report('30000000 tail calls') do
 -    tail(30000000)
 +    executeWithErrorHandling do
 +      tail(30000000)
 +    end
    end

    # Instance variables.
 -  bm.report('10000000 ivar read') { bench_ivar_read(10000000) }
 -  bm.report('30000000 ivar read') { bench_ivar_read(30000000) }
 -  bm.report('10000000 ivar write') { bench_ivar_write(10000000) }
 -  bm.report('30000000 ivar write') { bench_ivar_write(30000000) }
 +  bm.report('10000000 ivar read') do
 +    executeWithErrorHandling do
 +      bench_ivar_read(10000000)
 +    end
 +  end
 +  bm.report('30000000 ivar read') do
 +    executeWithErrorHandling do
 +      bench_ivar_read(30000000)
 +    end
 +  end
 +  bm.report('10000000 ivar write') do
 +    executeWithErrorHandling do
 +      bench_ivar_write(10000000)
 +    end
 +  end
 +  bm.report('30000000 ivar write') do
 +    executeWithErrorHandling do
 +      bench_ivar_write(30000000)
 +    end
 +  end

    # Const lookup.
    bm.report('30000000 const') do
 -    i=0; while i<30000000; i+=ConstantOne; end
 +    executeWithErrorHandling do
 +      i=0; while i<30000000; i+=ConstantOne; end
 +    end
    end

    # Blocks
    bm.report('30000000 yield') do
 -    o = Class1.new
 -    o.yield1(30000000) {}
 +    executeWithErrorHandling do
 +      o = Class1.new
 +      o.yield1(30000000) {}
 +    end
    end
    bm.report('30000000 msg w/ block+yield') do
 -    o = Class1.new
 -    i=0; while i<30000000; o.yield2 {}; i+=1; end
 +    executeWithErrorHandling do
 +      o = Class1.new
 +      i=0; while i<30000000; o.yield2 {}; i+=1; end
 +    end
    end
    bm.report('30000000 Proc#call') do
 -    o = proc {}
 -    i=0; while i<30000000; o.call; i+=1; end
 +    executeWithErrorHandling do
 +      o = proc {}
 +      i=0; while i<30000000; o.call; i+=1; end
 +    end
    end
    bm.report('30000000 dvar write') do
 -    i=0
 -    30000000.times { i=1 }
 +    executeWithErrorHandling do
 +      i=0
 +      30000000.times { i=1 }
 +    end
    end

    # Eval
    bm.report('1000 eval') do
 -    i=0
 -    s = "#{1+1}+#{20+20}"
 -    while i<1000
 -      eval(s)
 -      i+=1
 +    executeWithErrorHandling do
 +      i=0
 +      s = "#{1+1}+#{20+20}"
 +      while i<1000
 +        eval(s)
 +        i+=1
 +      end
      end
    end
    bm.report('30000000 binding-var write') do
 -    i=0
 -    eval('while i<30000000; i+=1; end')
 +    executeWithErrorHandling do
 +      i=0
 +      eval('while i<30000000; i+=1; end')
 +    end
    end

    # Break
    bm.report('30000000 while break') do
 -    i=0; while i<30000000; while true; i+=1; break; end; end
 +    executeWithErrorHandling do
 +      i=0; while i<30000000; while true; i+=1; break; end; end
 +    end
    end
    bm.report('10000000 block break') do
 -    i=0; while i<10000000; 1.times { i+=1; break }; end
 +    executeWithErrorHandling do
 +      i=0; while i<10000000; 1.times { i+=1; break }; end
 +    end
    end

    # Next
    bm.report('30000000 while next') do
 -    i=0; while i<30000000; i+=1; next; exit; end
 +    executeWithErrorHandling do
 +      i=0; while i<30000000; i+=1; next; exit; end
 +    end
    end
    bm.report('10000000 block next') do
 -    i=0; while i<10000000; 1.times { i+=1; next; exit; }; end
 +    executeWithErrorHandling do
 +      i=0; while i<10000000; 1.times { i+=1; next; exit; }; end
 +    end
    end

    # Exception handlers.
    bm.report('60000000 begin w/o exception') do
 -    i=0
 -    while i<60000000
 -      begin
 -        i+=1
 -      rescue
 +    executeWithErrorHandling do
 +      i=0
 +      while i<60000000
 +        begin
 +          i+=1
 +        rescue
 +        end
        end
      end
    end
    bm.report('60000000 ensure w/o exception') do
 -    i=0
 -    while i<60000000
 -      begin
 +    executeWithErrorHandling do
 +      i=0
 +      while i<60000000
          begin
 +          begin
 +          ensure
 +            i+=1
 +          end
          ensure
            i+=1
          end
 -      ensure
 -        i+=1
        end
      end
    end
    bm.report('50000 raise') do
 -    i=0
 -    while i<50000
 -      begin
 -        raise
 -      rescue
 -        i+=1
 +    executeWithErrorHandling do
 +      i=0
 +      while i<50000
 +        begin
 +          raise
 +        rescue
 +          i+=1
 +        end
        end
      end
    end

    # Method
    bm.report('3000000 Method#call w/ 0 arg') do
 -    o = Class1.new
 -    m = o.method(:method1)
 -    i=0
 -    while i<3000000
 -      m.call
 -      i+=1
 +    executeWithErrorHandling do
 +      o = Class1.new
 +      m = o.method(:method1)
 +      i=0
 +      while i<3000000
 +        m.call
 +        i+=1
 +      end
      end
    end
    bm.report('3000000 Method#call w/ 1 arg') do
 -    o = Class1.new
 -    m = o.method(:method2)
 -    i=0
 -    while i<3000000
 -      m.call(i)
 -      i+=1
 +    executeWithErrorHandling do
 +      o = Class1.new
 +      m = o.method(:method2)
 +      i=0
 +      while i<3000000
 +        m.call(i)
 +        i+=1
 +      end
      end
    end
 -
  end
 }}}

-- 
Ticket URL: <http://www.macruby.org/trac/ticket/324>
MacRuby <http://macruby.org/>



More information about the MacRuby-devel mailing list