How to Interpret Output of system.time Function in R

Jesse John Feb 02, 2024
  1. Understand the Terms in the Output of system.time Function in R
  2. Examples of the system.time Output in R
How to Interpret Output of system.time Function in R

The system.time() function is one of the tools that we can use to estimate how long it takes for a code to execute. Its output gives three values: user, system, and elapsed time in seconds.

After a brief explanation of the terms involved, we will see examples of the output of this function to understand what is measured by every value.

Understand the Terms in the Output of system.time Function in R

In the output of the system.time() function, the easiest term to understand is the third value: elapsed. It measures the clock time that passed while the code chunk ran.

In almost all cases, this matters to users: they have to wait for R to complete running the code.

The combination of the other two, user and system, measures the CPU’s time to execute the code chunk.

In simple terms, we can think of them as follows.

User time is CPU time taken to process the user application code (R code). If the user application accessed system resources, that processing time is reported as system time.

The Wikipedia articles given in the References section provide brief technical details about these two components of CPU time.

Examples of the system.time Output in R

The following trivial example shows how the system.time() function is used.

Example code:

system.time({
    print("Hello to R.")
})

Output:

[1] "Hello to R."
   user  system elapsed
  0.001   0.000   0.001

The system.time() function calls the proc.time() function before and after the code chunk, and gives us the difference.

It is equivalent to the following.

Example code:

x = proc.time()
print("Hello to R.")
y = proc.time()
y-x

Output:

> x = proc.time()
> print("Hello to R.")
[1] "Hello to R."
> y = proc.time()
> y-x
   user  system elapsed
  0.001   0.001   0.001

Look at the following two examples to understand the difference between user and system time.

In the first example, R calculates 1000 times and prints only once. Run this code chunk several times and check the results.

Example code:

system.time({
    x=0
    for(i in 1:1000){
        x = x+1
    }
    print(x)
})

Output:

   user  system elapsed
  0.009   0.000   0.011

While the user time is positive, the system time is almost 0 in most cases.

Now, let’s see another code example that performs the same calculation but prints 1000 times.

Example code:

system.time({
    x=1
    for(i in 1:1000){
        x = x+1
        print(x)
    }
})

Output:

   user  system elapsed
  0.093   0.026   0.120

Now, we find that the system time is also positive. This was because of the 1000 print statements, which made R access the operating system’s input-output resources.

Finally, to illustrate elapsed time, let us see an example where the CPU is idle for a long time.

Example code:

system.time({
    Sys.sleep(5) # Time in seconds.
})

Output:

   user  system elapsed
  0.013   0.006   5.000

We find that the elapsed time is much more than the CPU time. It is (almost) the same as the sleep time that we specified since the code did nothing else.

References

The following two Wikipedia articles give brief technical details related to CPU time.

  1. CPU time
  2. User space and kernel space
Author: Jesse John
Jesse John avatar Jesse John avatar

Jesse is passionate about data analysis and visualization. He uses the R statistical programming language for all aspects of his work.