Created
February 14, 2013 15:46
-
-
Save deric/4953652 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
require 'set' | |
n = 100000 | |
bench = {} | |
set = Set.new | |
array = Array.new | |
rand= Random.new | |
for i in 1..100 do | |
num = rand() | |
set << num | |
array << num | |
end | |
test = 10.times.map{ Random.rand() } | |
puts "array contents: " | |
puts array.to_s | |
puts "array size = #{array.size}, set size= #{set.size}" | |
puts "test: " << test.to_s | |
Benchmark.bm(2) do |x| | |
name = "array" | |
bench[name] = x.report(name) do | |
n.times do | |
test.each do |t| | |
array.include?(t) | |
end | |
end | |
end | |
name = "set" | |
bench[name] = x.report(name) do | |
n.times do | |
test.each do |t| | |
set.include?(t) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
array 5.700000 0.010000 5.710000 ( 5.706463)
set 0.300000 0.000000 0.300000 ( 0.303586)
so almost 20 times faster for me (ruby 1.9.2p320 OSX mavericks 2.66 Ghz Intel Core 2 Duo)