Skip to content

Instantly share code, notes, and snippets.

@ernie
Created July 16, 2011 20:46
Show Gist options
  • Save ernie/1086770 to your computer and use it in GitHub Desktop.
Save ernie/1086770 to your computer and use it in GitHub Desktop.
A benchmark of some enumerable methods
require 'benchmark'
haystack = (1..1_000_000).to_a
needles = 1.upto(100).map {|n| n * 10_000}
module EachDetector
def self.find(haystack, needle)
haystack.each do |v|
return true if v == needle
end
false
end
end
puts "\n" + '=' * 80
puts "With in-block needle calculation"
puts '=' * 80
Benchmark.bmbm(10) do |x|
x.report("detect") do
100.times do |n|
haystack.detect {|v| v == 10_000 * n}
end
end
x.report("each") do
100.times do |n|
EachDetector.find(haystack, 10_000 * n)
end
end
x.report("any") do
100.times do |n|
haystack.any? {|v| v == 10_000 * n}
end
end
x.report("include") do
100.times do |n|
haystack.include?(10_000 * n)
end
end
end
puts "\n" + '=' * 80
puts "With precalculated needles"
puts '=' * 80
Benchmark.bmbm(10) do |x|
x.report("detect") do
needles.each do |n|
haystack.detect {|v| v == n}
end
end
x.report("each") do
needles.each do |n|
EachDetector.find(haystack, n)
end
end
x.report("any") do
needles.each do |n|
haystack.any? {|v| v == n}
end
end
x.report("include") do
needles.each do |n|
haystack.include?(n)
end
end
end
MRI 1.9.2 (ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0])
================================================================================
With in-block needle calculation
================================================================================
user system total real
detect 7.650000 0.000000 7.650000 ( 7.654394)
each 5.380000 0.010000 5.390000 ( 5.391129)
any 7.330000 0.010000 7.340000 ( 7.330595)
include 2.620000 0.000000 2.620000 ( 2.614191)
================================================================================
With precalculated needles
================================================================================
user system total real
detect 6.400000 0.000000 6.400000 ( 6.396854)
each 5.360000 0.010000 5.370000 ( 5.353514)
any 6.040000 0.000000 6.040000 ( 6.039250)
include 2.620000 0.000000 2.620000 ( 2.619387)
JRuby (jruby 1.6.3 (ruby-1.8.7-p330) (2011-07-07 965162f) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_26) [darwin-x86_64-java])
================================================================================
With in-block needle calculation
================================================================================
user system total real
detect 4.736000 0.000000 4.736000 ( 4.736000)
each 3.080000 0.000000 3.080000 ( 3.080000)
any 4.755000 0.000000 4.755000 ( 4.755000)
include 0.390000 0.000000 0.390000 ( 0.390000)
================================================================================
With precalculated needles
================================================================================
user system total real
detect 3.052000 0.000000 3.052000 ( 3.052000)
each 3.127000 0.000000 3.127000 ( 3.127000)
any 3.667000 0.000000 3.667000 ( 3.666000)
include 0.389000 0.000000 0.389000 ( 0.389000)
Rubinius (rubinius 1.2.4dev (1.8.7 7ae451a1 yyyy-mm-dd JI) [x86_64-apple-darwin10.8.0])
================================================================================
With in-block needle calculation
================================================================================
user system total real
detect 6.751770 0.002679 6.754449 ( 6.754644)
each 3.440632 0.001522 3.442154 ( 3.442024)
any 6.438806 0.005987 6.444793 ( 6.445838)
include 0.729296 0.000179 0.729475 ( 0.729454)
================================================================================
With precalculated needles
================================================================================
user system total real
detect 5.665156 0.000725 5.665881 ( 5.666424)
each 3.280126 0.000350 3.280476 ( 3.280500)
any 5.589283 0.001355 5.590638 ( 5.590594)
include 0.729572 0.000174 0.729746 ( 0.729729)
MRI 1.8.7 (ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin10.8.0])
================================================================================
With in-block needle calculation
================================================================================
user system total real
detect 71.000000 0.040000 71.040000 ( 71.058398)
each 14.680000 0.000000 14.680000 ( 14.691505)
any 71.740000 0.050000 71.790000 ( 71.788846)
include 3.410000 0.000000 3.410000 ( 3.417467)
================================================================================
With precalculated needles
================================================================================
user system total real
detect 63.220000 0.070000 63.290000 ( 63.301757)
each 14.660000 0.000000 14.660000 ( 14.667927)
any 63.810000 0.050000 63.860000 ( 63.961712)
include 3.410000 0.000000 3.410000 ( 3.419042)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment