system.time() and proc.time()
Measure elapsed, user, and system time for R expressions.
Why Measure Performance?
Before optimizing code, you need to measure it. Guessing where slowness lives leads to wasted effort. R provides built-in tools to time expressions precisely.
The three main tools are system.time(), proc.time(), and Sys.time(). Each serves a different purpose in performance analysis.
system.time() Basics
system.time(expr) evaluates an expression and returns how long it took. It is the simplest way to time a single operation in R.
The result is an object of class proc_time with named elements you can inspect.
result <- system.time({
x <- 1:1000000
total <- sum(x)
})
print(result)All lessons in this course
- system.time() and proc.time()
- Profiling Code with Rprof and profvis
- Vectorization for Speed
- Benchmarking with microbenchmark