timeit: Profile a Function Call

Description Usage Arguments Details Value Note See Also Examples

Description

This is a wrapper to Rprof that cleans up some of the profile hand-holding and provides easier usage. This allows you to profile either a single function call, or a whole block. Evaluation can be run multiple times in order to assess variability in the timings of each function call.

Usage

1
2
3
  timeit(call, replications = NULL, interval = 0.01,
    memory.profiling = TRUE, times = 10,
    show.warnings = FALSE, plot = TRUE)

Arguments

call

a call; this can be a single function call or a whole block.

replications

integer; by default NULL, which indicates we should 'guess' an appropriate number of replications. in order to more accurately profile quickly-running functions, we run the call replications times, and then infer the run-time as <time> / replications. by default, the argument is NULL and we attempt to infer an appropriate number of replications.

interval

real. time interval between samples.

memory.profiling

logical. output memory usage statistics?

times

integer. how many times to call the function?

show.warnings

boolean. output a warning if any iteration of the run did not produce results?

plot

boolean; if TRUE we return a boxplot of the running times.

Details

Function calls that get executed very quickly will be missed by Rprof, unless you set interval very low. However, doing this will probably break things (and isn't really important, since profiling is there to help you catch the longest-running functions.) If you really want to time quickly executed functions, you can manually set the replications argument: we evaluate the call replications times, and infer the (average) run-time of the function as <time taken> / replications.

Value

An object of S3 classes timeit and data.frame.

Note

If you set the replications argument high, you will likely see some output from the do_timeout call that is unrelated to your function call. This is due to all the wrapping of a function call to be executed by Rprof introduces a minor overhead. For other caveats, please see Rprof.

Currently, timeit does not support passing through of arguments, so don't try to wrap timeit in a function call, whereby the call it attempts to evaluate is passed from a parent function. For example,

f <- function(x) { timeit(x) }; f(rnorm(10))

won't work properly; a fix may come in the future.

See Also

mean.timeit for mean running times over all iterations processed, summary.timeit for summary statistics, plot.timeit for generating a boxplot of the returned times, do_timeit for the workhorse function, and Rprof for information on how R profiles the execution of expressions.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## Not run: 
tmp <- timeit({
  x <- 1:1E4; y <- x + runif(1E4)
  lm( y ~ x )
  }, times=5)
summary(tmp)
y <- 1E4
f <- function(x) { summary( sort( rnorm(x) ) ) }
tmp <- timeit( f(y), times=5 )
if( !is.null(tmp) ) {
  summary(tmp)
  mean(tmp)
  if( require(ggplot2) ) { plot(tmp) }
}
## End(Not run)

kevinushey/timeit documentation built on May 20, 2019, 9:20 a.m.