Skip to content

Instantly share code, notes, and snippets.

@icebreaker
Created August 3, 2011 14:32
Show Gist options
  • Select an option

  • Save icebreaker/1122774 to your computer and use it in GitHub Desktop.

Select an option

Save icebreaker/1122774 to your computer and use it in GitHub Desktop.
"manual unique" vs "set"
require 'set'
a = []
(1..100000).each do |i|
a << rand(1000)
end
def benchmark
start = Time.now
yield
puts Time.now - start
end
u = []
puts 'array uniques'
benchmark do
a.each do |i|
u << i unless u.include? i
end
end
puts u.count
u = Set.new
puts 'set uniques'
benchmark do
a.each do |i|
u << i
end
end
puts u.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment