Skip to content

Instantly share code, notes, and snippets.

@deepak
Created December 14, 2010 10:20
Show Gist options
  • Select an option

  • Save deepak/740238 to your computer and use it in GitHub Desktop.

Select an option

Save deepak/740238 to your computer and use it in GitHub Desktop.
benchmark array membership in REE 1.8.7 2010-04-19 patchlevel 253 - 2010.02
# benchmark array membership in REE 1.8.7 2010-04-19 patchlevel 253 - 2010.02
# the basic access-pattern is like Array#include? - a big array and to check if a a smaller/single-object
# is included in the bigger one
# TODO: why is such a big difference between the two - irrespecive of the fact that both of them trying to do diff things
# array_intersect is checking if an array containing a single element is included? in a bigger array
# arr_ele_include is checking if an single integer array is included? in a bigger array
# array_intersect is the fastest followed by arr_ele_include
# ruby -v
# ruby 1.8.7 (2010-04-19 patchlevel 253) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2010.02
require 'benchmark'
require 'set'
ARR = (1..5000).to_a
ELE_ARR = [3301]
ELE = 3301
ARR_SET = ARR.to_set
ELE_SET = ELE_ARR.to_set
SO_MUCH = 10_000
Benchmark.bmbm { |x|
x.report("arr_include") {
SO_MUCH.times { ARR.include?(ELE_ARR) }
}
x.report("array_intersect") {
SO_MUCH.times { !(ARR && ELE_ARR).empty? }
}
x.report("arr_ele_include") {
SO_MUCH.times { ARR.include?(ELE) }
}
x.report("set_include") {
SO_MUCH.times { ARR_SET.include?(ELE_SET) }
}
x.report("set_intersect") {
SO_MUCH.times { ARR_SET.subset?(ELE_SET) }
}
}
__END__
# ruby 1.8.7 (2010-04-19 patchlevel 253) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2010.02
Rehearsal ---------------------------------------------------
arr_include 16.370000 0.020000 16.390000 ( 16.525376)
array_intersect 0.000000 0.000000 0.000000 ( 0.002414)
arr_ele_include 2.070000 0.000000 2.070000 ( 2.071033)
set_include 0.020000 0.000000 0.020000 ( 0.015478)
set_intersect 0.010000 0.000000 0.010000 ( 0.012009)
----------------------------------------- total: 18.490000sec
user system total real
arr_include 16.330000 0.080000 16.410000 ( 16.474641)
array_intersect 0.000000 0.000000 0.000000 ( 0.002331)
arr_ele_include 2.080000 0.000000 2.080000 ( 2.099532)
set_include 0.020000 0.000000 0.020000 ( 0.015422)
set_intersect 0.010000 0.000000 0.010000 ( 0.012076)
#ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
Rehearsal ---------------------------------------------------
arr_include 11.910000 0.000000 11.910000 ( 11.922087)
array_intersect 0.000000 0.000000 0.000000 ( 0.001565)
arr_ele_include 1.880000 0.000000 1.880000 ( 1.906705)
set_include 0.010000 0.000000 0.010000 ( 0.014681)
set_intersect 0.000000 0.000000 0.000000 ( 0.004333)
----------------------------------------- total: 13.800000sec
user system total real
arr_include 11.880000 0.070000 11.950000 ( 11.997317)
array_intersect 0.000000 0.000000 0.000000 ( 0.001642)
arr_ele_include 1.890000 0.000000 1.890000 ( 1.899360)
set_include 0.010000 0.000000 0.010000 ( 0.016123)
set_intersect 0.000000 0.000000 0.000000 ( 0.004484)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment