From: botp Date: 2008-09-11T09:43:38+09:00 Subject: Re: How to change the owner of a method in a block? On Wed, Sep 10, 2008 at 7:24 PM, Iņaki Baz Castillo wrote: > So using "instance_eval" is ~ 40% slower than running directly the code. well, we can try again :) botp@jedi-hopeful:~$ cat test.rb require 'benchmark' n=100_000 Benchmark.bmbm do |x| x.report("normal") { n.times { a=nil;a=123; a2=a*2; a3=a*3 }} x.report("string") { n.times {instance_eval 'a=nil;a=123; a2=a*2; a3=a*3' }} x.report("block") { n.times {instance_eval {a=nil;a=123; a2=a*2; a3=a*3} }} x.report("block-broken") { n.times {a=nil;instance_eval{a=123}; instance_eval{ a2=a*2}; instance_eval{a3=a*3} } } end botp@jedi-hopeful:~$ ruby test.rb Rehearsal ------------------------------------------------ normal 0.140000 0.000000 0.140000 ( 0.247775) string 2.420000 0.010000 2.430000 ( 3.438391) block 0.250000 0.000000 0.250000 ( 0.375656) block-broken 0.470000 0.010000 0.480000 ( 0.698093) --------------------------------------- total: 3.300000sec user system total real normal 0.120000 0.000000 0.120000 ( 0.216789) string 2.480000 0.020000 2.500000 ( 3.291342) block 0.300000 0.000000 0.300000 ( 0.423327) block-broken 0.490000 0.000000 0.490000 ( 0.703046) botp@jedi-hopeful:~$ slower but not slow (in my own mileage :) or optimistically, direct code is faster than a code block