LoginSignup
5
5

More than 5 years have passed since last update.

Array#count の実行時間について

Last updated at Posted at 2014-08-11

気になったので調べた。
検証コードはこんな感じ

array_size = ARGV[0].to_i

cup = array_size.times.inject([]){ |cup, i|
  cup << Random.rand(0..(i+1))
}

t1 = Time.now.usec
cup.count
t2 = Time.now.usec - t1

p "size : #{array_size} .. Time : #{t2}μs"

なんか雑だけどまぁそんな感じ。で、わかったこと

array_size μs
10 4
100 4
1,000 3
10,000 7
100,000 6
1,000,000 6
10,000,000 9
100,000,000 8

後半は配列を生成するほうが時間かかって辛い。
で、だいたい直感的には #count の実行時間(計算量?)はあんまり爆発しない。
詳しいこと(O()とか)とか知ってる方いたら、教えてください。


riocampos さん、ms -> us 修正ありがとうございます。

5
5
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5