library("profvis")
This will be a short post tonight. The topic is speeding up existing code. Typically the rule is make it work (e.g. do what you want it to do) before optimizing it. The trick then is that with some experience you can write your code so that you don’t box yourself into slow methods. That I won’t cover in this post.
The hard part is then to know what is slowing you down. Perhaps you have optimised everything that you know how to do, use purrr and apply statements, move some code to cpp. Now what next?
Enter profvis
Profvis provides a way to visualise the memory allocation and speed of each of your code. This includes simple things:
profvis({
<- lm(mpg ~ wt + disp , data = mtcars)
fit Sys.sleep(10)
<- predict(fit)
preds })
Now we can see that the plotting function takes the majority of the time.
This is a super trivial example, but knowledge of tools like this help for streamlining code, especially when pushing it to users. Sometimes you don’t know where the hang-up in your code is and this can help you understand what is happening.
Then you can pull in other tools like microbenchmark
or bench
to time the different methods of improvement.
Reuse
Citation
@online{dewitt2018,
author = {Michael DeWitt},
title = {Speed It Up!},
date = {2018-07-06},
url = {https://michaeldewittjr.com/programming/2018-07-06-speed-it-up},
langid = {en}
}