Skip to content

Instantly share code, notes, and snippets.

@murajun1978
Created September 6, 2014 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save murajun1978/9642a11c1bb536b2d500 to your computer and use it in GitHub Desktop.
Save murajun1978/9642a11c1bb536b2d500 to your computer and use it in GitHub Desktop.
Arrayのベンチマーク
require 'benchmark'
require 'set'
array = Array.new(1_000_000){[1, 2, 3]}
set = array.to_set
Benchmark.bm(15) do |x|
x.report('array:') { array.each do |n| n.include?(3) end }
x.report('set:') { set.each do |n| n.include?(3) end }
end
# 実行結果
user system total real
array: 0.200000 0.000000 0.200000 ( 0.203315)
set: 0.000000 0.000000 0.000000 ( 0.000010)
@springaki
Copy link

参考になりました !!
こうなると to_set のコストも知っておきたい所ですね。

x.report('to_set:')   { array.to_set }
# => to_set:           1.680000   0.000000   1.680000 (  1.688967)

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