[#39863] forループの速度 — Masahiro Sato <msato@...>

15 messages 2004/07/20

[#39868] イテレータとfor文 — OOTANI TAKASHI <otn@...5.so-net.ne.jp>

大谷と申します。

31 messages 2004/07/20
[#39886] Re: イテレータとfor文 — Tietew <tietew-ml-ruby-list@...> 2004/07/21

[ruby-list:39863] forループの速度

From: Masahiro Sato <msato@...>
Date: 2004-07-20 10:20:55 UTC
List: ruby-list #39863
佐藤と申します。

forループの速度についてなのですが、
下記のようなCのサンプルと比較すると、それぞれ下記のように
なります。(実際にやりたいことは配列の初期化ではなく、
ちょっとしたデータの生成です)

データの量が大きくなると、rubyではかなり時間がかかるので
どうにかしたいのですが、なにか良い方法がありましたら
教えてください。

それでは。

=====================

#!/usr/bin/env ruby

a = Array.new(100000)

for i in 0..1000
for j in 0..40000
  a[i] = 0
end
end

% time ./test.rb 
range.rb  32.83s user 0.11s system 100% cpu 32.933 total

======================

int main(){

  int i,j;
  int a[100000];
  
  for(i=0;i<=1000;i++){
    for(j=0;j<=40000;j++){
      a[i] = 0;
    }
  }

}

% time ./a.out
a.out  0.27s user 0.01s system 102% cpu 0.273 total

In This Thread

Prev Next