From: gotoken@... (GOTO Kentaro) Date: 1999-01-25T00:46:30+09:00 Subject: [ruby-list:11695] Re: Numeric#step の増減方向 ごとけんです ぼくは単にn回繰り返ししたいときは Integer のメソッドを 呼ぶのも変な気がするので def repeat(limit) raize "missing block" unless iterator? 1.step(limit,1){res = yield} res end と,見たくないものは隠してるのですが, 油断すると for i in 1..n と書いてしまいます(^^;; In message "[ruby-list:11690] Re: Numeric#step の増減方向" on 99/01/24, Sinichiro Dezawa writes: > ところで、step +1 でよい時はどっちちの方が軽いのでしょう? 空(カラ)ループを回したらこんなん出ました: Ruby-1.2.2 >>> 100000 times: step: utime => 0.233333, stime => 0.0 for: utime => 0.516667, stime => 0.0 >>> 1000000 times: step: utime => 2.25, stime => 0.0 for: utime => 5.16667, stime => 0.0 >>> 10000000 times: step: utime => 22.4333, stime => 0.0 for: utime => 51.7333, stime => 0.0 Ruby-1.3 >>> 100000 times: step: utime => 0.1, stime => 0.0 for: utime => 0.133333, stime => 0.0 >>> 1000000 times: step: utime => 1.1, stime => 0.0 for: utime => 1.36667, stime => 0.0 >>> 10000000 times: step: utime => 10.8833, stime => 0.0 for: utime => 13.75, stime => 0.0 ってわけで,1.2 だと倍くらい step が速いみたいです. もっとも,空ループですので,実際は気にならないかも知れません. しかし 1.3 は速いですね.ぱちぱちぱちぱち # 使ったプログラムを最後につけときます. -- gotoken print "Ruby-", VERSION, "\n" for n in [100000,1000000] a = Time.times 1.step(n,1){} b = Time.times for i in 1..n; end c = Time.times print ">>> #{format('%8d', n)} times:\n" print " step: utime => #{b.utime - a.utime}, " print "stime => #{b.stime - a.stime}\n" print " for: utime => #{b.utime - a.utime}, " print "stime => #{b.stime - a.stime}\n" end