# 14 How to check code efficiency

At some point, we may feel frustrated about our code—it takes so long to run! Generally speaking, it is a difficult task to make our code very efficient, but it is easy to find the elapsed time of code running and the bottleneck(s).

The tool that we can use is:

library(profvis)
profvis({R code})

Example:

library(profvis)
profvis({
x <- rnorm(10e8)
total_1 <- 0
for(i in 1:10e8) total_1 <- total_1 + x[i]
total_2 <- sum(x)
list(total_1, total_2)
})

As expected, the above analysis shows that the bottleneck is at the “for loop”.

For more details, please watch Winston Chang’s webinar https://www.rstudio.com/resources/webinars/profvis-profiling-tools-for-faster-r-code/ and his another talk https://www.rstudio.com/resources/videos/profiling-and-performance/