僕のYak Shavingは終わらない

車輪の再発明をやめたらそこには壮大なYakの群れが

rubyのループ(loop, each, while, for)の速度のベンチマーク

2014/12/16 15:14 追記
========
なんかはてブされていたので、現在の手元のRubyで検証してみた。

$ ruby -v
ruby 2.1.4p265 (2014-10-27 revision 48166) [x86_64-darwin14.0]
$ ruby bench.rb
code : while = 0.05[sec]
code : times = 0.08[sec]
code : for = 0.08[sec]
code : each = 0.09[sec]
code : loop = 0.11[sec]

まあloopが一番遅いのは変わらないですね。

↓の結果は2012年のものです。ご注意ください。
========


さっそく結果

# 速い順
code : while = 0.17[sec]
code : times = 0.23[sec]
code : each = 0.24[sec]
code : for = 0.27[sec]
code : loop = 0.43[sec]

forがeachよりも遅いのは一度構文解析してeachに直しているから?(そんなことしてるかはわからないけど)
あと今回whileが爆速だけど継続条件を判定する部分が複雑になると遅くなるのでその辺注意しないとwhileでもかなり遅くなると思う。
参考程度にしておいてください。

benchに使ったcode

#!/usr/bin/env ruby
# encoding : utf-8
require 'benchmark'
max = 1_000_000
result = {}  
Benchmark.bm do |x|
  result[:for] = x.report("for ") { 
    ary = []
    for count in 0..max 
      ary << count
    end
    p ary[-1]
  }
  result[:each] = x.report("each ") {
    ary = [] 
    (0..max).each do |count|
      ary << count
    end
    p ary[-1]
  }
  result[:times] = x.report("times ") {
    ary = []
    (max + 1).times do |count|
      ary << count
    end
    p ary[-1]
  }
  result[:while] = x.report("while ") {
    ary = []
    count = 0
    while count <= max
      ary << count
      count += 1 
    end 
    p ary[-1] 
  }   
  result[:loop] = x.report("loop ") {
    ary = []
    count = 0
    loop do
      if count <= max
        ary << count 
        count += 1
      else
        break
      end
    end
    p ary[-1]
  } 
end   
result.sort_by{|key, value| value.real}.map{|key, value| print "code : #{key} = " + "%0.2f" % value.real + "[sec]\n"}