Is user + sys time equal to real/wall clock time?

Tag: unix Author: snrgw91924 Date: 2009-10-27

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?

Best Answer

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

comments:

Good point... it doesn't count sleep()... I believe I knew that, but for some reason couldn't think of that... Thanks.

Other Answer1

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.

Other Answer2

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

Other Answer3

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.