Learn R Programming

timeit (version 0.2.1)

timeit: Profile a Function Call

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

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

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 / 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?

Value

An object of S3 classes timeit and data.frame.

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 .

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

Run this code
## 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)

Run the code above in your browser using DataLab