tags:

views:

149

answers:

4

In Perl:

my ($usr,$sys,$cusr,$csys) = times;
printf "real %s, user %s, sys %s\n", $usr + $sys, $usr, $sys;

Is this true? I read some other questions where people did things like saving the current time away in a variable and collecting it later, but why wouldn't this work?

+4  A: 
C:\Temp> cat tt.pl
sleep 5;
my ($user, $system) = times;
print "u = $user s = $system\n";


C:\Temp> tt
u = 0.015 s = 0.015
Sinan Ünür
Good point... it doesn't count sleep()... I believe I knew that, but for some reason couldn't think of that... Thanks.
xyld
+4  A: 

No. User time and system time is something else.

my $start = time;
sleep 5;
my ($usr,$sys,$cusr,$csys) = times;
my $stop = time;
printf "Real time (approx): %gs  User+sys: %gs\n", $stop-$start,$usr+$sys;


Real time (approx): 5s  User+sys: 0.077s
mobrule
+9  A: 

No, it is not. User and system time account only for used CPU time. While the program is asleep, real time still passes, but the user and system time stand still. Also, on multi-CPU systems, it may well happen that user+system is greater than real time.

zvrba
+2  A: 

Although a few people have showed you examples with sleep, also consider the real-life cases where your process is waiting for responses from remote machines, other processes, and so on. While you are waiting for your database query, wallclock time is marching on even as you aren't using the processor.

brian d foy