Categories
Linux Tutorial Series

Linux Tutorial Series – 107 – Measuring how much time it takes for your program to execute

Here is the video version, if you prefer it:

If you are a software developer, you sometimes want to know how much time does your program need to execute. Or, if you are a “regular” Linux user, maybe you want to know how much time a command takes to execute (albeit most likely not). Here is how to measure it: (Ward, 2014)⁠

time programName

An example:

mislav@mislavovo-racunalo:~$ time ls

...

real 0m0.004s

user 0m0.000s

sys 0m0.004s

There are 3 relevant times: (“What do ‘real’, ‘user’ and ‘sys’ mean in the output of time(1)?,” n.d.)⁠

  • real time – the amount of CPU time from starting the call to finishing the call of the process; this includes the time your process waited for some resource and the time the processor was executing other processes (your processor can switch to another process and execute a fraction of that process, then get back to the execution of your process)
  • user time – the amount of CPU time spent within the process
  • sys time – the amount of CPU time spent in the kernel within the process; if you wanted to do some stuff that only the kernel can do (remember that a regular user can’t do everything), you call the kernel function to do that and then this gets added up to sys time

The CPU time your process takes up is user + sys time.

References

Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 178-179

What do “real”, “user” and “sys” mean in the output of time(1)? (n.d.). Retrieved February 14, 2020, from https://stackoverflow.com/questions/556405/what-do-real-user-and-sys-mean-in-the-output-of-time1