| Next | Making Programs Faster | 27 |
CPU time is measured with the built-in times() function
($u, $s, $cu, $cs) = times();
$u and $s are the user and system CPU times consumed by this process
$cu and $cs are the CPU time consumed by descendant processes of this one
(These are unavailable on Windows systems)
# busyloop
use Time::HiRes 'time';
($parent_run, $child_run) = @ARGV;
$start = time;
until (time >= $start + $parent_run) {
# Busy loop
}
if (fork) { # parent
wait;
} else { # child
$start = time;
until (time >= $start + $child_run) {
# Busy loop
}
exit;
}
printf (<<EOF, times());
u: %.2f s: %.2f
cu: %.2f cs: %.2f
EOF
% ./busyloop 6 2
u: 5.11 s: 0.88
cu: 1.61 cs: 0.40
Most benchmarking tools are based on times
| Next | ![]() |
Copyright © 2003 M. J. Dominus |