Skip to content

Instantly share code, notes, and snippets.

@ahoward
Forked from banker/set_benchmark.rb
Created August 30, 2012 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahoward/3533460 to your computer and use it in GitHub Desktop.
Save ahoward/3533460 to your computer and use it in GitHub Desktop.
Which of these is the fastest?
x = 10000.times.to_a
a,b,c = x.dup, x.dup, x.dup
Benchmark.measure { 1000.times { out = []; out += a; out += b; out +=c; out.uniq! } }
Benchmark.measure { 1000.times { out = Set.new; out += a; out += b; out +=c; } }
Benchmark.measure { 1000.times { out = {}; a.each {|e| out[e] ||= 1}; b.each {|e| out[e] ||= 1}; c.each {|e| out[e] ||= 1}; } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment