Skip to content

Instantly share code, notes, and snippets.

@vessi
Created May 10, 2012 20:39
Show Gist options
  • Save vessi/2655741 to your computer and use it in GitHub Desktop.
Save vessi/2655741 to your computer and use it in GitHub Desktop.
benchmark for found array "or" sum
require 'benchmark'
GC.disable
@array = []
@array_size = 3000000
@iterations = 100
def prepopulate_array(mode)
@array = Array.new(@array_size, false)
if mode == 'first'
@array[0] = true
elsif mode == 'last'
@array[-1] = true
elsif mode == 'random'
@array[rand @array_size] = true
elsif mode == 'group'
(rand @array_size).times do
@array[rand @array_size] = true
end
end
end
['first', 'last', 'random', 'group'].each do |mode|
puts "#{mode} element"
Benchmark.bm(15) do |x|
x.report('prepopulation') do
prepopulate_array(mode)
end
x.report("uniq.inject") do
@iterations.times { @array.uniq.inject{|result, element| result or element} or false }
end
x.report("include?") do
@iterations.times { @array.include?(true) }
end
x.report("any?") do
@iterations.times { @array.any? }
end
x.report("find") do
@iterations.times { !!@array.find{ |el| el } }
end
x.report("count") do
@iterations.times { @array.count(true) > 0 }
end
end
end
@vessi
Copy link
Author

vessi commented May 10, 2012

first element
                      user     system      total        real
prepopulation     0.000000   0.000000   0.000000 (  0.007910)
uniq.inject      18.920000   0.000000  18.920000 ( 18.939562)
include?          0.000000   0.000000   0.000000 (  0.000015)
any?              0.000000   0.000000   0.000000 (  0.000032)
find              0.000000   0.000000   0.000000 (  0.000067)
count            11.560000   0.000000  11.560000 ( 11.584380)
last element
                      user     system      total        real
prepopulation     0.000000   0.010000   0.010000 (  0.007431)
uniq.inject      18.750000   0.000000  18.750000 ( 18.777156)
include?         11.770000   0.000000  11.770000 ( 11.785240)
any?              6.080000   0.000000   6.080000 (  6.083509)
find             20.590000   0.000000  20.590000 ( 20.619887)
count            11.440000   0.000000  11.440000 ( 11.460866)
random element
                      user     system      total        real
prepopulation     0.010000   0.010000   0.020000 (  0.007413)
uniq.inject      18.870000   0.000000  18.870000 ( 18.903051)
include?          0.660000   0.000000   0.660000 (  0.659995)
any?              0.350000   0.000000   0.350000 (  0.346236)
find              1.190000   0.000000   1.190000 (  1.193930)
count            11.680000   0.000000  11.680000 ( 11.692200)
group element
                      user     system      total        real
prepopulation     0.650000   0.000000   0.650000 (  0.659253)
uniq.inject      21.100000   0.000000  21.100000 ( 21.125556)
include?          0.000000   0.000000   0.000000 (  0.000012)
any?              0.000000   0.000000   0.000000 (  0.000042)
find              0.000000   0.000000   0.000000 (  0.000039)
count             7.270000   0.000000   7.270000 (  7.283692)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment