The log method is called after the task has finished. A log call with no
argument will record the start time of a task, otherwise, the start time
of a task is same as the finish time of last task.
yabm.rb:
class Yabm
def initialize
@cp=[] @cp << [nil,Time.now]
end
def log(label=nil) @cp << [label,Time.now]
end
def report
total=@cp[@cp.length-1][1]-@cp[0][1]
caption=“Time used: %0.2f sec.”%total
print “\n”,caption,"\n"
print "-"caption.length,"\n" @cp.each_index do |i|
if @cp[i][0]!=nil then
elapsed=@cp[i][1]-@cp[i-1][1]
printf “%s: %0.2f sec. (%0.2f%%)\n”, @cp[i][0],elapsed,elapsed/total100
end
end
end
end
A moron question: what is the meaning of “user time” and “system time”?
Is it useful when doing benchmarking?
what is the meaning of “user time” and “system time”?
Is it useful when doing benchmarking?
User time is the amount of CPU time spent within the user-space (your
program), and system time is the amount of CPU time spent within the
kernel (syscall calls). It is useful in determining where the program
is spending most of its time.
I/O bound programs are usually spend more time in syscall
calls, CPU bound programs in user-space.
CPU bound programs can usually be improved by using a more efficient
algorithm and minimising side-activities (e.g. creation of
intermediate objects, etc). I/O bound programs can be improved by
optimising the I/O access pattern, and of course by upgrading the
I/O hardware if that is possible.
User time is the amount of CPU time spent within the user-space (your
program), and system time is the amount of CPU time spent within the
kernel (syscall calls). It is useful in determining where the program
is spending most of its time.
wallclock time is usually the best measure. It catches all those place where
the CPU stalls.
Typically a program is not slow becuase of what it is doing, but because of
what it is not doing.
CPU cache misses easily cost 1000 instructions and much more if stalling on
virtual memory on disk.
(I believe this is a major reason why Java doesn’t beat the crap out of Ruby
in performance).
The wallclock time will benefit small code where the instruction cache isn’t
hit too bad and it will also benefit programs that doesn’t trash all over
the memory causing expensive cache misses. And of course bad disk I/O
patterns are also measured here.
The downside of wallclock time is that you also measure background activity
from other processes, but you just find a quiet moment on the computer
before starting to measure.