Skip to content

Instantly share code, notes, and snippets.

@ainame
Forked from takai/1. uniq_bench.rb
Created May 9, 2012 17:58
Show Gist options
  • Select an option

  • Save ainame/2647349 to your computer and use it in GitHub Desktop.

Select an option

Save ainame/2647349 to your computer and use it in GitHub Desktop.
正しそうな結果が得られるように変更しました
user system total real
uniq 0.760000 0.000000 0.760000 ( 0.761028)
uniq! 0.010000 0.000000 0.010000 ( 0.009451)
set 0.010000 0.000000 0.010000 ( 0.005617)
hash 0.000000 0.000000 0.000000 ( 0.001273)
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment