Speed it up!

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

Michael DeWitt https://michaeldewittjr.com

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:

  fit <- lm(mpg ~ wt + disp , data = mtcars)
  preds <- predict(fit)

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.


If you see mistakes or want to suggest changes, please create an issue on the source repository.


Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/medewitt/medewitt.github.io, unless otherwise noted. 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 ...".


For attribution, please cite this work as

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

BibTeX citation

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