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.
正しそうな結果が得られるように変更しました
# -*- coding: utf-8 -*-
require 'benchmark'
require 'set'
# 1回の配列の生成に対して100回の平均をとる
n = 100
# 配列の要素の最大値が100...100000まで比較
[100, 1000, 10000, 100000, 1000000, 10000000].each do |max|
# 5回のベンチの平均
Benchmark.bm(5) do |x|
# 要素数100000個の乱数の配列に対して操作
source = 100000.times.map { rand(max) }
p "max_value: #{max}"
x.report('uniq') do n.times { source.dup.uniq.size } end
x.report('uniq!') do n.times { source.dup.uniq! } end
x.report('set') do n.times { Set.new(source.dup) } end
x.report('hash') do n.times { source.dup.each_with_object(Hash.new) {|i, h| h[i] }.keys } end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment