Measuring elapsed time

Problem

You want to measure how much time it takes to run a particular block of code.

Solution

The system.time() function will measure how long it takes to run something in R.

  1. system.time({
  2. # Do something that takes time
  3. x <- 1:100000
  4. for (i in seq_along(x)) x[i] <- x[i]+1
  5. })
  6. #> user system elapsed
  7. #> 0.144 0.002 0.153

The output means it took 0.153 seconds to run the block of code.