Informal profiling with tictoc
code
beginner
r
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()
<- Sys.time()
tic # code here
<- Sys.time()
toc 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()