Skip to content

Instantly share code, notes, and snippets.

@heftig
Created February 27, 2012 20:22
Show Gist options
  • Save heftig/1926783 to your computer and use it in GitHub Desktop.
Save heftig/1926783 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'set'
array = (1..10000).to_a
set = array.to_set
probes = 10000.times.map { rand(20000) }
Benchmark.bmbm do |bm|
bm.report("Array") { probes.each { |p| array.include? p } }
bm.report("Set") { probes.each { |p| set.include? p } }
end
~/ ruby set.rb
Rehearsal -----------------------------------------
Array 3.050000 0.000000 3.050000 ( 3.060505)
Set 0.010000 0.000000 0.010000 ( 0.002117)
-------------------------------- total: 3.060000sec
user system total real
Array 2.400000 0.000000 2.400000 ( 2.406458)
Set 0.000000 0.000000 0.000000 ( 0.001736)
~/ rbx set.rb
Rehearsal -----------------------------------------
Array 0.889865 0.001000 0.890865 ( 0.892081)
Set 0.007998 0.000000 0.007998 ( 0.007734)
-------------------------------- total: 0.898863sec
user system total real
Array 0.861869 0.000000 0.861869 ( 0.863451)
Set 0.006999 0.000000 0.006999 ( 0.007077)
~/ jruby set.rb
Rehearsal -----------------------------------------
Array 0.189000 0.000000 0.189000 ( 0.189000)
Set 0.030000 0.000000 0.030000 ( 0.029000)
-------------------------------- total: 0.219000sec
user system total real
Array 0.117000 0.000000 0.117000 ( 0.117000)
Set 0.024000 0.000000 0.024000 ( 0.024000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment