Informal profiling with tictoc

code
beginner
r
Author

Daniel Kick

Published

June 25, 2020

The tictoc library has convenience functions for timing code. Here’s the basic usage relative to timing with base R.

library(tictoc)
tic()
# code here
toc()

tic <- Sys.time()
# code here
toc <- Sys.time()
print(toc - tic)

Where this library excels is when you want to time multiple parts of your code. Each tic pushes the time onto a stack and each toc pops the most recent time from said stack. That means you don’t have to worry about assigning several timing variables even if you want to time nested code.

tic()
# stack is 1 deep
for (i in 1:10) {
     tic()
     # stack is now 2 deep
     for (j in 1:10){
          tic()
          # stack is now 3 deep
          toc()
     }
     toc()
}
toc()