Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | Rubyのプロファイラ

概要

Rubyのプロファイラ

詳細

Rubyのプロファイラを扱う方法について。

サンプルコード

require 'profile'

LOOP_COUNT = 1000
TEST_NUMBER_LIST = (1..100).to_a
TEST_STRING_LIST = ("1".."100").to_a

def summary_number(list)
  list.inject(:+)
end

def summary_string(list)
  list.inject {|sum, cnt|sum.to_i + cnt.to_i}
end

LOOP_COUNT.times {|x|summary_number(TEST_NUMBER_LIST)}
LOOP_COUNT.times {|x|summary_string(TEST_STRING_LIST)}

実行結果

  %   cumulative   self              self     total
 time   seconds   seconds    calls  ms/call  ms/call  name
 68.44     1.65      1.65     2000     0.83     1.17  Array#each
 12.28     1.95      0.30    98000     0.00     0.00  Integer#to_i
  9.64     2.19      0.23    99000     0.00     0.00  Fixnum#+
  6.41     2.34      0.15   100000     0.00     0.00  String#to_i
  1.32     2.37      0.03     2000     0.02     1.19  Enumerable.inject
  1.28     2.40      0.03        2    15.50  1209.00  Integer#times
  0.62     2.42      0.01     1000     0.01     0.73  Object#summary_number
  0.00     2.42      0.00        2     0.00     0.00  Range#each
  0.00     2.42      0.00        2     0.00     0.00  Module#method_added
  0.00     2.42      0.00        1     0.00     0.00  String#upto
  0.00     2.42      0.00        1     0.00     0.00  String#<=>
  0.00     2.42      0.00        2     0.00     0.00  Enumerable.to_a
  0.00     2.42      0.00     1000     0.00     1.65  Object#summary_string
  0.00     2.42      0.00        1     0.00  2418.00  #toplevel
[Finished in 2.6s]