Benchmarking with microbenchmark
Compare multiple implementations statistically with microbenchmark().
Why microbenchmark?
system.time() has millisecond resolution and is unreliable for fast operations. The microbenchmark package runs expressions hundreds of times, handles warm-up, and reports nanosecond-resolution statistics — making it the right tool for comparing similar implementations.
Basic microbenchmark Usage
Pass named expressions to microbenchmark(). Each argument name becomes the label in the output. The times argument controls how many times each expression is evaluated.
# library(microbenchmark)
# x <- 1:10000
#
# microbenchmark(
# loop = {
# s <- 0
# for (v in x) s <- s + v
# },
# vectorized = sum(x),
# times = 200L
# )All lessons in this course
- system.time() and proc.time()
- Profiling Code with Rprof and profvis
- Vectorization for Speed
- Benchmarking with microbenchmark