[#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:39873] Re: forループの速度

From: Masahiro Sato <msato@...>
Date: 2004-07-20 23:36:02 UTC
List: ruby-list #39873
質問した佐藤です。

ささださん、岩室さん、教えていただきありがとう
ございます。

具体的になにをやりたいかというと、
ファイルからデータを読みこみ、そのデータをちょっと
いじって(定数倍するとか)出力するということです。

岩室さんのご指摘のようにwhileをつかってやってみると、

#!/usr/bin/env ruby

a = Array.new(100000)

i = 0
j = 0

while i < 1000
  while j < 40000
    a[i] = 0
    j += 1
  end
  i += 1
end


% time ./test2.rb # while
test2.rb  0.06s user 0.03s system 250% cpu 0.036 total

forより500倍ぐらい速くなりました。

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

% time ./a.out   # C言語
a.out  0.27s user 0.01s system 102% cpu 0.273 total

というかC言語より5倍ほど速くなっています。

そういうものなのでしょうか。

In This Thread