On Jan 1, 2006, at 8:12 AM, Sven Johansson wrote:


>> require 'benchmark'
>>
>> REP = 1_000_000
>>
>> def foo(n) 0 + n end
>>
>> Benchmark.bmbm(10) do |bm|
>>   bm.report("direct") do
>>     REP.times { x = 0 + 1 }
>>   end
>>
>>   bm.report("method") do
>>     REP.times { x = foo(1) }
>>   end
>> end
>>
>
> Ah, interesting. And a fine exemple of how to use the internal
> benchmarking support for us Ruby newbies. More to the point, it shows
> that it isn't so bad - I was thinking of order-of-magnitude losses,  
> and
> here is merely a factor two or so, and that's for essentially empty
> method bodies... it will do. :)
>

No, you discovered the difference between 1 method invocation (Fixnum. 
+) and 2 (Kernel.foo and Fixnum.+). "0 + 1" is a method invocation  
just like any other. You can see that by using ParseTree's  
parse_tree_show utility:

	[:call, [:lit, 0], :+, [:array, [:lit, 1]]]

If you are worried about times, I'd look at using a good profiler  
instead of the benchmarks so you can get insight on where your time  
is actually being spent (it sure isn't on Fixnum.+). Don't use the  
standard profiler, use zenprofiler or shugo's profiler.

--
ryand-ruby / zenspider.com - http://blog.zenspider.com/
http://rubyforge.org/projects/ruby2c/
http://rubyforge.org/projects/rubyinline/