Speed it up!

This post explores how to see opportunities to make your code run faster.

Michael DeWitt https://michaeldewittjr.com
07-06-2018

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


library("profvis")

Profvis provides a way to visualise the memory allocation and speed of each of your code. This includes simple things:


profvis({
  fit <- lm(mpg ~ wt + disp , data = mtcars)
  preds <- predict(fit)
  plot(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

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

DeWitt (2018, July 6). Michael DeWitt: Speed it up!. Retrieved from https://michaeldewittjr.com/dewitt_blog/posts/2018-07-06-speed-it-up_files/

BibTeX citation

@misc{dewitt2018speed,
  author = {DeWitt, Michael},
  title = {Michael DeWitt: Speed it up!},
  url = {https://michaeldewittjr.com/dewitt_blog/posts/2018-07-06-speed-it-up_files/},
  year = {2018}
}