The calendar is gone.
Click here to view posts


Different includes
So there are many different ways to tell if an array includes an object. The results will change depending on where in the array the object is and if the object is in the array or not. A I once again used a random array to benchmark a few ways to do this.
n = 10000
start_array = Array.new(6000)
(0...6000).each{|i| start_array[i]=rand(4000000)}
require "benchmark"
Benchmark.bm(1) do |test|
  test.report("include?:") do
    n.times{
      start_array.include?(5)
    }
  end
  test.report("for loop:") do
    n.times{
      found = false
      for x in start_array
        if x == 5
          found = true
          break 
        end
      end
     }
  end
  test.report("any:") do
    n.times{
     start_array.any?{|x| x==5}
    }
  end
  test.report("find:") do
    n.times{
     true if start_array.find{|x| x==5}
    }
  end
  test.report("select:") do
    n.times{
    true if start_array.select{|x| x==5}
    }
  end
end
The results?
#         user     system      total        real 
#select:  52.730000   0.400000  53.130000 ( 53.264748)
#include?: 12.180000   0.020000  12.200000 ( 12.222207)
#for loop: 44.630000   0.100000  44.730000 ( 44.863327)
#any: 52.890000   0.450000  53.340000 ( 53.563788)
#find: 43.590000   0.350000  43.940000 ( 43.999914)


I ran the test again but this time I inserted 5 at index 200. No shock they all cut out when they find the object they are looking for.
#       user     system      total        real
#include?:  0.410000   0.010000   0.420000 (  0.449498)
#for loop:  1.500000   0.010000   1.510000 (  1.545805)
#any:  1.780000   0.000000   1.780000 (  1.777178)
#find:  1.770000   0.000000   1.770000 (  1.773765)
#select: 43.750000   0.400000  44.150000 ( 44.936202)