Dan Debertin <airboss / nodewarrior.org> writes:

>       t = yield i

>       t = block.call(i)
 
> Is this just TMTOWTDI, or is there a difference?

There's an internal difference, in that 'yield' does not create a proc
object, while (&block) does. This might make a performance
difference. Let's find out...


     require "benchmark"
     include Benchmark

     N = 500000

     def block(&b)
       N.times { b.call(1) }
     end

     def no_block
       N.times { yield 1 }
     end

     bm(10) do |test|

       test.report("block")    { block {|i|}    }
       test.report("no block") { no_block {|i|} }
       test.report("block")    { block {|i|}    }
       test.report("no block") { no_block {|i|} }

     end

     dave[~/tmp 22:58:57] ruby t.rb
                     user     system      total        real
     block       3.430000   0.000000   3.430000 (  3.450126)
     no block    1.320000   0.000000   1.320000 (  1.383497)
     block       3.450000   0.000000   3.450000 (  3.450232)
     no block    1.330000   0.000000   1.330000 (  1.329939)


Yup - there's a difference :)


Cheers


Dave