【发布时间】:2015-08-25 19:58:50
【问题描述】:
让我们以docs为例:
require 'profile'
def slow_method
5000.times do
9999999999999999*999999999
end
end
def fast_method
5000.times do
9999999999999999+999999999
end
end
slow_method
fast_method
输出:
% cumulative self self total
time seconds seconds calls ms/call ms/call name
68.42 0.13 0.13 2 65.00 95.00 Integer#times
15.79 0.16 0.03 5000 0.01 0.01 Fixnum#*
15.79 0.19 0.03 5000 0.01 0.01 Fixnum#+
0.00 0.19 0.00 2 0.00 0.00 IO#set_encoding
0.00 0.19 0.00 1 0.00 100.00 Object#slow_method
0.00 0.19 0.00 2 0.00 0.00 Module#method_added
0.00 0.19 0.00 1 0.00 90.00 Object#fast_method
0.00 0.19 0.00 1 0.00 190.00 #toplevel
-
% time是在这些方法中花费了多少时间。 -
cumulative seconds是之前的cumulative seconds加上self seconds,即0 + 0.13 = 0.13、0.13 + 0.03 = 0.16、0.16 + 0.03 = 0.19等等。 -
self seconds是% time,以秒为单位。 -
calls表示该方法被调用了多少次。 -
self ms/call是self seconds/calls。 - 什么是
total ms/call?
【问题讨论】:
-
我看到
(130/0.6842)/2 #=> 95.001...可能是个线索? -
@CarySwoveland 计算不适合其他行。我会说
total ms/call表示自第一个堆栈从顶层进入而不是从最近的外部堆栈进入以来每次方法调用的时间(以毫秒为单位) - 那是self ms/call。表中没有基值,只有调用次数,因此从中推导出公式没有意义。 -
看来这是正确的意思:Profiler source
-
又是一个gprof clone。它说
Profiling your program is a way of determining which methods are called and how long each method takes to complete. This way you can detect which methods are possible bottlenecks.错了。 -
我确实打算试试
ruby-prof。只是想澄清一下,关于这个。