Skip to content

Instantly share code, notes, and snippets.

@takai
Created April 26, 2012 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save takai/2503006 to your computer and use it in GitHub Desktop.
Save takai/2503006 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'set'
source = 100000.times.map { rand(100) }
n = 100
Benchmark.bm(5) do |x|
x.report('uniq') do n.times { source.uniq } end
x.report('uniq!') do n.times { source.uniq! } end
x.report('set') do n.times { Set.new(source) } end
x.report('hash') do n.times { source.each_with_object(Hash.new) {|i, h| h[i] }.keys } end
end
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin11.3.0]
user system total real
uniq 0.770000 0.000000 0.770000 ( 0.777292)
uniq! 0.010000 0.000000 0.010000 ( 0.011109)
set 0.010000 0.000000 0.010000 ( 0.003713)
hash 0.000000 0.000000 0.000000 ( 0.001277)
jruby 1.6.7 (ruby-1.9.2-p312) (2012-02-22 3e82bc8) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_31) [darwin-x86_64-java]
user system total real
uniq 0.555000 0.000000 0.555000 ( 0.555000)
uniq! 0.015000 0.000000 0.015000 ( 0.015000)
set 0.208000 0.000000 0.208000 ( 0.208000)
hash 0.089000 0.000000 0.089000 ( 0.090000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment