In order to benchmark chunks,
I implemented a
hook
for Rmd’s chunk which works as if Jupyter Notebook’s
%%timeit
command.
Implementation
A simple benchmark is done by saving proc.time()
before running code and subtract the value from new proc.time()
after code runs.
t <- proc.time()['elapsed']
invisible(runif(100))
proc.time()['elapsed'] - t
## elapsed
## 0.006
However, hooks are functions, and bodies of functions are evaluated in different environments every time.
f <- function() environment()
c(f(), f())
## [[1]]
## <environment: 0x563a7b3ceb08>
##
## [[2]]
## <environment: 0x563a7b3ce910>
Thus, an implementing hook function has to know environment to save proc.time()
, and to call the saved proc.time()
.
Fortunately, the hook with envir
argument enables it because envir
captures an environment in which a code chunk is evaluated.
In addition, the hook needs to know if they are called before or after a code chunk, which is possible by using before
argument.
Finally, the hook is implemented as below.
library(knitr)
knit_hooks$set(
time = function(before, envir) {
t <- proc.time()['elapsed']
if(before) {
envir$.elapsed <- t
} else {
paste(signif(t - envir$.elapsed), 'sec.')
}
}
)
Use this hook by setting time = TRUE
as a chunk option.
FYI, returning value must be a character to be printed, and
signif()
is used to focus on significant digits.
Test
```{r, time = TRUE}
head(x)
```
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
0.041 sec.
Great!
Question
Is there any way to see benchmark result right after evaluating chunks in RStudio’s editor? I see the results after knitting.
Tip
If you are using R Notebook, just preview it and you can see benchmark results.