On Thu, Aug 21, 2008 at 1:38 PM, Robert Klemme
<shortcutter / googlemail.com> wrote:
<snip>
> But then again, you could directly use #find or even better #include?.

In case of ranges #include? really should be your choice, it seems to
be optimized as one
might have expected....


511/12 > cat find-include.rb && ruby find-include.rb
# vim: sw=2 ts=2 ft=ruby expandtab tw=0 nu:

require 'benchmark'


R = (1..1_000)
N = 5
Benchmark::bmbm do |bm|
  bm.report("find") do
    N.times do
      R.each do |ele|
        R.find{ |e| e == ele}
        R.find{ |e| e.zero? }
      end
    end
  end
  bm.report("include?") do
    N.times do
      R.each do |ele|
        R.include? ele
        R.include? 0
      end
    end
  end
end
Rehearsal --------------------------------------------
find       6.797000   0.000000   6.797000 (  6.828000)
include?   0.000000   0.000000   0.000000 (  0.016000)
----------------------------------- total: 6.797000sec

               user     system      total        real
find       6.438000   0.000000   6.438000 (  6.828000)
include?   0.016000   0.000000   0.016000 (  0.016000)
HTH
Robert
> --
> use.inject do |as, often| as.you_can - without end
endless loops might run for quite some time though ;)


-- 
http://ruby-smalltalk.blogspot.com/

There's no one thing that's true. It's all true.
--
Ernest Hemingway