Learn R Programming

Smisc (version 0.3.9.1)

timeIt: Times the execution of an expression

Description

Times the execution of an expression

Usage

timeIt(expr, units = c("automatic", "seconds", "minutes", "hours", "days"),
  return.time = FALSE, verbose = TRUE)

Arguments

expr

Any R expression.

units

A character expression long enough to uniquely identify one of "automatic", "seconds", "minutes", or "hours". Defaults to "automatic".

return.time

= TRUE returns the elapsed time as one of the elements in a list. See "Value" below.

verbose

= TRUE prints the elapsed time in the requested units.

Value

If return.time = FALSE, invisibly returns the evaluation of expr. If return.time = TRUE, invisibly returns a list with the following components:

out

The evaluation of expr

elapsed

The elapsed time to evaluate expr

units

The time units of the elapsed time

Details

If units = "automatic", then the units are choosen according to the following rule: If the duration is < 2 min, use seconds. Else if duration < 2 hours, use minutes. Else if < 2 days, use hours. Otherwise, use days.

See Also

proc.time

Examples

Run this code
# NOT RUN {
# We can assign the object within the call to timeIt():
timeIt(x1 <- rnorm(10^6))
str(x1)

# }
# NOT RUN {
# We can just run the expression without assigning it to anything
timeIt(rnorm(10^7), units = "m")

# Or we can assign the result externally
x2 <- timeIt(rnorm(10^7))
str(x2)

# To store the elapsed time:
x3 <- timeIt(rnorm(10^7), verbose = FALSE, return.time = TRUE)
x3[c("elapsed","units")]
# }

Run the code above in your browser using DataLab