Skip to content

Instantly share code, notes, and snippets.

@meinac
Created June 22, 2015 16:27
Show Gist options
  • Save meinac/0f4806f6c812b65db4d8 to your computer and use it in GitHub Desktop.
Save meinac/0f4806f6c812b65db4d8 to your computer and use it in GitHub Desktop.
Array.uniq.to_set vs Array.to_set
require 'benchmark/ips'
require 'set'
#init array
arr = Array.new
1000.times do
arr << (rand * 20).to_i
end
Benchmark.ips do |x|
x.config(:time => 10, :warmup => 2)
x.report("Array") do
arr.uniq.to_set
end
x.report("Set") do
arr.to_set
end
x.compare!
end
@meinac
Copy link
Author

meinac commented Jun 22, 2015

Comparison:
               Array:    23503.0 i/s
                 Set:     5785.4 i/s - 4.06x slower

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