In the later case, to calculate the CPU usage ratio, you can use something like that:
my ($cpu_time, $clock_time) = (0, 0);
sub your_event_processing {
my ($start_user, $start_system) = times;
my $start_clock = time;
# do the event processing here
# ...
my ($end_user,$end_system) = times;
my $end_clock = time;
$clock_time += ($end_clock - $start_clock);
$cpu_time += ($end_user - $start_user + end_system - $start_system);
}
END {
printf STDERR "clock_time: %d, cpu_time: %d (%4.2f%%)\n",
$clock_time, $cpu_time, $cpu_time * 100 / $clock_time;
}
And let it run for some hundreds of events, so that the rounding errors go away. |