Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discrepancy in Linux time command output

Tags:

linux

time

I am aware that the output of the time command can show greater time under the user section than the real section for multi-processor cases, but recently, I was trying to profile a program when I saw that real was substantially greater than user + sys.

$ time ./test.o

real    0m5.576s
user    0m1.270s
sys     0m0.540s

Can anybody explain why such a behaviour is caused?

like image 456
susmits Avatar asked Jun 16 '11 06:06

susmits


People also ask

What is the output of time command in Linux?

The output of the time command also includes the CPU seconds the executable uses in the user and the kernel modes. So, in the above example, the CPU time spent in the user mode was 0.001 seconds and the CPU time spent in the kernel mode was 0.002 seconds.

How does time command work in Linux?

time command in Linux is used to execute a command and prints a summary of real-time, user CPU time and system CPU time spent by executing a command when it terminates.

What is user time in time command?

3.1. These are executed in kernel mode. All other commands are executed in user mode. user time represents the time that a command has executed in user mode. In our case, that was precisely 0.063 seconds.

What is user and sys time?

'user' time is the CPU time spent in user-mode code (outside the kernel). 'Sys' time is the amount of CPU time spent in the kernel. This means executing CPU time spent in system calls within the kernel, as opposed to library code, which is still running in user-space.


2 Answers

That's the normal behavior.

"Real" is is the wall-clock time. In your example, it literally took 5.576 seconds to run './test.o'

'user' is the User CPU time, or (roughly) CPU time used by user-space processes. This is essentially the time your CPU spent actually executing './test.o'. 1.270 seconds.

And finally, 'sys' is System CPU time, or (roughly) CPU time used by your kernel. 0.540 seconds.

If you add sys + user, you get the amount of time your CPU had to spend executing the program.

real - (user + sys) is, then, the time spent not running your program. 3.766 seconds were spent between invocation and termination not running your program--probably waiting for the CPU to finish running other programs, waiting on disk I/O, etc.

like image 68
Flimzy Avatar answered Sep 22 '22 06:09

Flimzy


Time your process spends sleeping (e.g., waiting for I/O) is not counted by either "user" or "system", but "real" time still elapses.

Try:

time cat

...then wait 10 seconds and hit ctrl-D.

like image 34
Nemo Avatar answered Sep 25 '22 06:09

Nemo