What Are 'User' and 'System' Times Measuring in R System.Time(Exp) Output

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

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._

what do user , system , and elapsed times mean in R

If you do help(system.time) you get a hint to also look at help(proc.time). I quote from its help page:

Value:

 An object of class ‘"proc_time"’ which is a numeric vector of
length 5, containing the user, system, and total elapsed times for
the currently running R process, and the cumulative sum of user
and system times of any child processes spawned by it on which it
has waited. (The ‘print’ method uses the ‘summary’ method to
combine the child times with those of the main process.)

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._

Times of child processes are not available on Windows and will
always be given as ‘NA’.

The resolution of the times will be system-specific and on
Unix-alikes times are rounded down to milliseconds. On modern
systems they will be that accurate, but on older systems they
might be accurate to 1/100 or 1/60 sec. They are typically
available to 10ms on Windows.

R code for system.time() user time extraction

try the double brackets.

i[["elapsed"]]

Or

i[["user.self"]] if that's what you want, which one are you after?

Or, take the name off the value:

avalue<-i[i]#your example with the name attached
names(avalue)=NULL; #now avalue is just a number again.

Timing R code with Sys.time()

As soon as elapsed time exceeds 1 minute, the default unit changes from seconds to minutes. So you want to control the unit:

while (difftime(Sys.time(), tm, units = "secs")[[1]] < period)

From ?difftime

 If ‘units = "auto"’, a suitable set of units is chosen, the
largest possible (excluding ‘"weeks"’) in which all the absolute
differences are greater than one.

Subtraction of date-time objects gives an object of this class, by
calling ‘difftime’ with ‘units = "auto"’.

Alternatively use proc.time, which measures various times ("user", "system", "elapsed") since you started your R session in seconds. We want "elapsed" time, i.e., the wall clock time, so we retrieve the 3rd value of proc.time().

period <- 10
tm <- proc.time()[[3]]
while (proc.time()[[3]] - tm < period) print(proc.time())

If you are confused by the use of [[1]] and [[3]], please consult:

  • How do I extract just the number from a named number (without the name)?
  • How to get a matrix element without the column name in R?

Let me add some user-friendly reproducible examples. Your original code with print inside a loop is quite annoying as it prints thousands of lines onto the screen. I would use Sys.sleep.

test.Sys.time <- function(sleep_time_in_secs) {
t1 <- Sys.time()
Sys.sleep(sleep_time_in_secs)
t2 <- Sys.time()
## units = "auto"
print(t2 - t1)
## units = "secs"
print(difftime(t2, t1, units = "secs"))
## use '[[1]]' for clean output
print(difftime(t2, t1, units = "secs")[[1]])
}

test.Sys.time(5)
#Time difference of 5.005247 secs
#Time difference of 5.005247 secs
#[1] 5.005247

test.Sys.time(65)
#Time difference of 1.084357 mins
#Time difference of 65.06141 secs
#[1] 65.06141

The "auto" units is very clever. If sleep_time_in_secs = 3605 (more than an hour), the default unit will change to "hours".

Be careful with time units when using Sys.time, or you may be fooled in a benchmarking. Here is a perfect example: Unexpected results in benchmark of read.csv / fread. I had answered it with a now removed comment:

You got a problem with time units. I see that fread is more than 20 times faster. If fread takes 4 seconds to read a file, read.csv takes 80 seconds = 1.33 minutes. Ignoring the units, read.csv is "faster".

Now let's test proc.time.

test.proc.time <- function(sleep_time_in_secs) {
t1 <- proc.time()
Sys.sleep(sleep_time_in_secs)
t2 <- proc.time()
## print user, system, elapsed time
print(t2 - t1)
## use '[[3]]' for clean output of elapsed time
print((t2 - t1)[[3]])
}

test.proc.time(5)
# user system elapsed
# 0.000 0.000 5.005
#[1] 5.005

test.proc.time(65)
# user system elapsed
# 0.000 0.000 65.057
#[1] 65.057

"user" time and "system" time are 0, because both CPU and the system kernel are idle.

Accessing user time returned from system.time

Just use the name with brackets. Here's an example.

 a <- system.time(rnorm(50000)) 

user system elapsed
0.003 0.000 0.004

names(a)
[1] "user.self" "sys.self" "elapsed" "user.child" "sys.child"

a["user.self"]
user.self
0.003

R system.time() as a number

The tt object is proc_time class, and is actually a Named numeric. So, basically a named vector.

unname(tt[3])

Or as @Matthew Plourde mentioned

tt[['elapsed']]

can be also used.

How to get elapsed time with system.time?

We can do

system.time(log(1:1e5))["elapsed"]
#elapsed
# 0.002

To only get the numerical value do

system.time(log(1:1e5))[["elapsed"]]

Is Sys.time() the time required for the individual computer to run commands?

Another way is to use proc.time; e.g.

> start_time <- proc.time()
> # some computation
> for (i in 1:10000000) i ^ 3
> proc.time() - start_time
user system elapsed
0.70 0.02 0.72


Related Topics



Leave a reply



Submit