Skip to content

Instantly share code, notes, and snippets.

@MikeSilvis
Last active December 19, 2015 19:08
Show Gist options
  • Save MikeSilvis/6003776 to your computer and use it in GitHub Desktop.
Save MikeSilvis/6003776 to your computer and use it in GitHub Desktop.
Benchmarking Ruby's Set .include? Vs Array .include?. The results are incredible.
➲ ruby set.rb "500"
user system total real
0.150000 0.000000 0.150000 ( 0.149935)
0.150000 0.000000 0.150000 ( 0.153456)
0.000000 0.000000 0.000000 ( 0.000085)
0.000000 0.000000 0.000000 ( 0.000081)
1.9.3 Desktop
➲ ruby set.rb "5000"
user system total real
1.440000 0.000000 1.440000 ( 1.440795)
1.490000 0.010000 1.500000 ( 1.493820)
0.000000 0.000000 0.000000 ( 0.000739)
0.000000 0.000000 0.000000 ( 0.000698)
1.9.3 Desktop
➲ ruby set.rb "50000"
user system total real
15.020000 0.080000 15.100000 ( 15.103763)
14.810000 0.010000 14.820000 ( 14.807303)
0.000000 0.000000 0.000000 ( 0.006720)
0.010000 0.000000 0.010000 ( 0.006420)
require 'set'
require 'benchmark'
array = (0..10000).to_a
set = (0..10000).to_set
n = ARGV[0].to_i
Benchmark.bm do |x|
x.report { n.times { array.include?(9090) } }
x.report { n.times { array.include?(9090) } }
x.report { n.times { set.include?(9090) } }
x.report { n.times { set.include?(9090) } }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment