Skip to content

Instantly share code, notes, and snippets.

@PatrickTulskie
Created September 27, 2013 03:20
Show Gist options
  • Save PatrickTulskie/6723731 to your computer and use it in GitHub Desktop.
Save PatrickTulskie/6723731 to your computer and use it in GitHub Desktop.
Performance testing Ruby's uniq
require 'rubygems'
require 'benchmark'
objects = ["string", %w(array), { :key => 'value' }, 9000] * 100
GC.disable
Benchmark.bm do |x|
x.report('map => uniq') do
10000.times { objects.map { |object| object.class }.uniq }
end
x.report('uniq => map') do
10000.times { objects.uniq { |object| object.class }.map { |object| object.class } }
end
end
# Ruby Enterprise Edition
# user system total real
# map => uniq 0.990000 0.010000 1.000000 ( 1.010733)
# uniq => map 1.680000 0.020000 1.700000 ( 1.690783)
#
# Ruby 1.9.3 w/ performance patches
# user system total real
# map => uniq 2.170000 0.010000 2.180000 ( 2.175190)
# uniq => map 1.440000 0.000000 1.440000 ( 1.436704)
#
# Ruby 2.0
# user system total real
# map => uniq 0.930000 0.010000 0.940000 ( 0.941251)
# uniq => map 0.620000 0.000000 0.620000 ( 0.618595)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment