How to interpret time "real", "user" and "sys"
Long story short, it simply means that your program didn't request to perform any privileged tasks, hence CPU has spend no time in the privileged (kernel) mode.
Basics First
First of all, there's several things you need to understand in order to interpret the output of time
properly:
- which
time
command you're running (because there's multiple) - the meaning of each field and what each one of them means (extra reading here)
- the difference between kernel and user mode
There's two types time
command. There's shell built-in and there's /usr/bin/time
. The shell built in is the one that you're using and it defaults to showing 3 lines, real
, user
,and sys
. The manual for time
also mentions this same form of output:
-p, --portability Use the following format string, for conformance with POSIX standard 1003.2: real %e user %U sys %S
And if you check the "FORMATTING THE OUTPUT" section we have:
E Elapsed real (wall clock) time used by the process, in [hours:]minutes:seconds. U Total number of CPU-seconds that the process used directly (in user mode), in seconds. S Total number of CPU-seconds used by the system on behalf of the process (in kernel mode), in seconds.
From all this info we can gather that there's two modes in which CPU can run - user mode and kernel mode, and the time
command displays how long CPU remains in a given mode to do whatever your program/app has asked CPU to do.
Understanding what is sys time reporting and what does 0 value mean
OK, so we understand the difference is that there's user mode and kernel mode in which code can run. We already can figure that this basically means the program didn't use kernel mode for executing some tasks. What does that actually mean ?
Refer to this post:
In Kernel mode, the executing code has complete and unrestricted access to the underlying hardware. It can execute any CPU instruction and reference any memory address.
And from another answer on the same post:
The switch from user mode to kernel mode is not done automatically by CPU. CPU is interrupted by interrupts (timers, keyboard, I/O). When interrupt occurs, CPU stops executing the current running program, switch to kernel mode, executes interrupt handler. This handler saves the state of CPU, performs its operations, restore the state and returns to user mode.
So that's what your output means - there was no switch to kernel mode and CPU did not receive any interrupts from the program to do so. This also means that your code doesn't have anything that would require elevated privileges from CPU
Note: this probably should be rephrased as "there was no long/noticeable switch into kernel mode", since allocating memory with something like malloc()
function in C would require switching into kernel mode, but these are microscopic switches, which we'd really have to investigate with a debugger or strace
command.
Related videos on Youtube
![WinEunuuchs2Unix](https://i.stack.imgur.com/2SXNl.jpg?s=256&g=1)
WinEunuuchs2Unix
Software development is my main hobby. Check out the new websites created in October 2021: www.pippim.com and pippim.github.io
Updated on September 18, 2022Comments
-
WinEunuuchs2Unix almost 2 years
Introduction
I've been fine-tuning code to reduce processing from 30 seconds to under a second in various parts of my bash program. I'm having trouble wrapping my mind about how the
time
command works when it reportsreal
,user
andsys
variables.I have this code:
echo " " echo "Time to build DirsNdxArr from DirsArr $DirsArrCnt elements:" DirsArrCnt=${#DirsArr[@]} time for (( i=1; i<$DirsArrCnt; i=i+$DaElementCnt )); do DirsNdxArr["${DirsArr[$i]}"]=$i AllItemSizes=$(( $AllItemSizes + ${DirsArr[$(( $i + $ColFileSizes - 1 ))]} )) done echo " " echo "Time to build FilesNdxArr from FilesArr $FilesArrCnt elements:" FilesArrCnt=${#FilesArr[@]} time for (( i=0; i<$FilesArrCnt; i=i+$FaElementCnt )); do FilesNdxArr["${FilesArr[$i]}"]=$i AllTagSizes=$(( $AllTagSizes + ${FilesArr[$(( $i + $FaColFileSizes ))]} )) done
That reports this:
Time to build DirsNdxArr from DirsArr 56700 elements: real 0m0.149s user 0m0.149s sys 0m0.000s Time to build FilesNdxArr from FilesArr 390 elements: real 0m0.002s user 0m0.002s sys 0m0.000s
Why is
sys
time reporting zero?Interpreting the output of
time
builtin command one would assume the system is doing nothing but surely this isn't what is happening?ps I know
\n
can be used as a new-line toecho
with-e
parameter. My habit is to sacrifice one liner-cuteness and fringe arguments in favor of readability.-
muru about 7 yearsDid you read stackoverflow.com/questions/556405/…?
-
WinEunuuchs2Unix about 7 years@muru I did check for duplicates in Ask Ubuntu but I must admit I did not search Stack Overflow.
-
muru about 7 yearsOr Google, for that matter.
-
WinEunuuchs2Unix about 7 years@muru Yes I did use google and thought AU authors could gain a little lime-light.
-
muru about 7 yearsSo this questions is posted just for the sake of posting questions?
-
WinEunuuchs2Unix about 7 yearsUm noo... I'm long past the point of gaining points for asking a question solely for the point of asking a question. This is a real life application and I really am curious. It's not a mission critical question though.
-
Sergiy Kolodyazhnyy about 7 yearsIt's an OK of a question. Might be useful to people
-
muru about 7 yearsIf you really are curious, why no research? "I did use google and thought AU authors could gain a little lime-light" ... what does that even mean, if you're not looking for a chance to gain rep, or provide somebody else rep? And the last time AU had a massive voting reversal, people lost 20K+ rep.
-
muru about 7 yearsAt any rate, my -1 for lack of research will stand.
-
WinEunuuchs2Unix about 7 years@muru yes I'm aware of a few cases of 20k to 35k rep loss in AU by a couple or few I've chatted with in AU. I also posted a comment about steven or someone who lost >100k rep in another SE site. I'm not in collusion with Serg if that is your allegation. I appreciate your right to negative vote me and although it might cause minor pain it will not have lasting damage.
-
muru about 7 yearsYes, that's the sad fact. Questions without any research at all getting upvoted, rendering legitimate downvotes useless.
-
WinEunuuchs2Unix about 7 years@muru How
stackoverflow
answers are written for professional programmers are not the same asAskUbuntu
answers should be written in my opinion. There are two different groups and I don't see any problem with tailoring Q&A to two different audiences. Could I have posted a question and answered it myself for this audience and done it again for Stack Overflow audience. Sure. Should I do that all the time? No. I truly like to hear what other people think. I enjoy reading answers within the different demographics too. There is more than one way to skin a cat as they said in the 1960's. -
muru about 7 yearsI don't see what those things has to do with anything I said. Did I ask you to post and answer a question? No. I don't see any tailoring at all, so I don't see how that's relevant. Now, should you show basic research when posting a question? Yes. And that holds true irrespective of what you like or enjoy.
-
WinEunuuchs2Unix about 7 yearsAU and some other SE sites encourage users to "share their knowledge". Following that rule of thumb I could have spent an hour googling, learning and then writing a question and answer which, as you read my history I've done many times. But in this instance (for whatever reasons I had) I wanted to read what others had to say about this question. It's not a crime.
-
muru about 7 yearsYou could have read that simply by Googling, since others have already written a lot about it. An hour Googling? Nonsense.
-
muru about 7 yearsAnd that's all I have to say about this.
-
WinEunuuchs2Unix about 7 yearsI put that right up there with Abeconomics and how the economy has 180'd and they are short of employees in Japan and how they and the US will do a definitive action against Democratic Republic of Korea. Tempest in a Teapot.
-
-
muru about 7 yearsThere would be some switches (for allocating memory - but that's typically very fast).