Skip to content

Instantly share code, notes, and snippets.

@deric
Created February 14, 2013 15:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deric/4953652 to your computer and use it in GitHub Desktop.
Save deric/4953652 to your computer and use it in GitHub Desktop.
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
@joshuapinter
Copy link

Can you post a sample out of this to get a feeling of the comparison without running it? Thanks!

@imme5150
Copy link

    user     system      total        real

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment