14.2 Tools for profiling
14.2.1 Profiling R code
{profvis} and the “flame graph”
library(profvis)
function(){
top <-# We use profvis::pause() because Sys.sleep() doesn't
# show in the flame graph
pause(0.1)
# Running a series of function with lapply()
lapply(1:10, function(x){
* 10
x
})# Calling a lower level function
middle()
}
function(){
middle <-# Pausing before computing, and calling other functions
pause(0.2)
1e4 * 9
bottom_a()
bottom_b()
}
# Both will pause and print, _a for 0.5 seconds,
# _b for 2 seconds
function(){
bottom_a <-pause(0.5)
print("hey")
} function(){
bottom_b <-pause(2)
print("hey")
}profvis({
top()
})
Memory:
profvis does indicate memory used, but …
{profmem}
14.2.1.2 Benchmarking R code
""“never start optimizing if you cannot benchmark this optimization”""
Recommend
keep a notebook of different implementations that were attempted (for the bottlenecks in your app)
{bench}
- for comparing different implementations
- checks that outputs are the same
# Multiplying each element of a vector going from 1 to size
# with a for loop
function(size){
for_loop <- numeric(size)
res <-for (i in 1:size){
i * 10
res[i] <-
}return(res)
}# Doing the same thing using a vectorized function
function(size){
vectorized <-1:size) * 10
(
} bench::mark(
res <-for_loop = for_loop(1000),
vectorized = vectorized(1000),
iterations = 1000
)
14.2.2 Profiling {shiny}
14.2.2.2 Shiny front-end
See: https://engineering-shiny.org/need-for-optimization.html#b.-shiny-front-end