On Sep 2, 2006, at 2:18 AM, Ondrej Bilka wrote:

> I wondered what gives you optimalization
>
> if (foo.instance_of?(bar)) foo =foo.to_bar;
>
> istead foo=barconvert(foo);
>
> So I did quick benchmark
>
> require 'benchmark'
> Benchmark.bm{|b|
> b.report{1000000.times{|i|Fixnum;i.to_i}}
> b.report{1000000.times{|i|i.instance_of?(Fixnum)}}
> }
> And first was bit better.
> I put Fixnum in first test because without took first test 0.6s and  
> second 0.8s
> So it makes your code slower.

Something is wrong with your benchmark.

$ ruby t.rb
Rehearsal ---------------------------------------------------------
i.to_i                  1.970000   0.010000   1.980000 (  2.304494)
Fixnum; i.to_i          2.510000   0.010000   2.520000 (  2.933719)
i.instance_of? Fixnum   2.550000   0.010000   2.560000 (  2.868521)
------------------------------------------------ total: 7.060000sec

                             user     system      total        real
i.to_i                  1.960000   0.010000   1.970000 (  2.217043)
Fixnum; i.to_i          2.510000   0.010000   2.520000 (  3.011366)
i.instance_of? Fixnum   2.560000   0.010000   2.570000 (  2.834019)

-- 
Eric Hodel - drbrain / segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com