system.time {base}R Documentation

CPU Time Used

Description

Return CPU (and other) times that expr used.

Usage

system.time(expr, gcFirst = TRUE)
unix.time(expr, gcFirst = TRUE)

Arguments

expr Valid R expression to be “timed”
gcFirst Logical - should a garbage collection be performed immediately before the timing? Default is TRUE.

Details

system.time calls the builtin proc.time, evaluates expr, and then calls proc.time once more, returning the difference between the two proc.time calls.

unix.time is an alias of system.time, for compatibility reasons.

Timings of evaluations of the same expression can vary considerably depending on whether the evaluation triggers a garbage collection. When gcFirst is TRUE a garbage collection (gc) will be performed immediately before the evaluation of expr. This will usually produce more consistent timings.

Value

A numeric vector of length 5 containing the user cpu, system cpu, elapsed, subproc1, subproc2 times. The subproc times are the user and system cpu time used by child processes (and so are usually zero).
The resolution of the times will be system-specific; it is common for them to be recorded to of the order of 1/100 second, and elapsed time is rounded to the nearest 1/100.

Note

It is possible to compile R without support for system.time, when the function will throw an error.

See Also

proc.time, time which is for time series.

Examples

require(stats)
system.time(for(i in 1:100) mad(runif(1000)))
## Not run: 
exT <- function(n = 1000) {
  # Purpose: Test if system.time works ok;   n: loop size
  system.time(for(i in 1:n) x <- mean(rt(1000, df=4)))
}
#-- Try to interrupt one of the following (using Ctrl-C / Escape):
exT()                 #- about 3 secs on a 1GHz PIII
system.time(exT())    #~ +/- same
## End(Not run)

[Package base version 2.2.1 Index]