What are 'user' and 'system' times measuring in R system.time(exp) output?

I am using system.time(expression) to measure execution time for an R function.

The output I get for the call

system.time(myfunction())

is:

    user  system elapsed   
  117.36    5.65  127.86

What does 'user' and 'system' measure?


Solution 1:

This is discussed in ?proc.time (system.time() returns an object of class "proc.time"):

Details:

     ‘proc.time’ returns five elements for backwards compatibility, but
     its ‘print’ method prints a named vector of length 3.  The first
     two entries are the total user and system CPU times of the current
     R process and any child processes on which it has waited, and the
     third entry is the ‘real’ elapsed time since the process was
     started.

....and

Value:

....

     The definition of ‘user’ and ‘system’ times is from your OS.
     Typically it is something like

     _The ‘user time’ is the CPU time charged for the execution of user
     instructions of the calling process. The ‘system time’ is the CPU
     time charged for execution by the system on behalf of the calling
     process._

Solution 2:

The clearest explanation I've ever read on the difference between user and system elapsed time was provided by William Dunlap on [R-help]:

"User CPU time" gives the CPU time spent by the current process (i.e., the current R session) and "system CPU time" gives the CPU time spent by the kernel (the operating system) on behalf of the current process. The operating system is used for things like opening files, doing input or output, starting other processes, and looking at the system clock: operations that involve resources that many processes must share.

Although ?proc.time returns something similar, this description was a lot easier to understand for me.

Solution 3:

Here is some simple explanations:

Elapsed Time is the time charged to the CPU(s) for the expression.

User Time is the wall clock time. The time that you as a user experienced.

Usually both times are relatively close. But they may vary in some other situations. For example:

  • If elapsed time > user time, this means that the CPU is waiting around for some other operations (may be external) to be done.
  • If elapsed time < user time, this means that your machine has multiple cores and is able to use them

Solution 4:

Since these are generic anyway, from Wikipedia:

The term 'user CPU time' can be a bit misleading at first. To be clear, the total time (real CPU time) is the combination of the amount of time the CPU spends performing some action for a program and the amount of time the CPU spends performing system calls for the kernel on the program's behalf. When a program loops through an array, it is accumulating user CPU time. Conversely, when a program executes a system call such as exec or fork, it is accumulating system CPU time.

http://en.wikipedia.org/wiki/Time_(Unix)#User_Time_vs_System_Time

Solution 5:

Since those time variables are defined by your OS, you can retrieve information on how they are calculated by executing man time in your shell (on Unix):

...These statistics consist of (i) the elapsed real time between invocation and termination, (ii) the user CPU time (the sum of the tms_utime and tms_cutime values in a struct tms as returned by times(2)), and (iii) the system CPU time (the sum of the tms_stime and tms_cstime values in a struct tms as returned by times(2)).

The definition of the mentioned time variables can be found here:

tms_utime User CPU time.

tms_stime System CPU time.

tms_cutime User CPU time of terminated child processes.

tms_cstime System CPU time of terminated child processes.

A a clarification of the differences between user and system time is described in daroczig's answer and elsewhere on SO:

The tms_utime element is the amount of time spent executing your code, or the code in the C library. The tms_stime element is the amount of time spent in the kernel executing code on your behalf.