Speed it up!

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


July 6, 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


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.



BibTeX citation:
  author = {Michael DeWitt},
  title = {Speed It Up!},
  date = {2018-07-06},
  url = {https://michaeldewittjr.com/programming/2018-07-06-speed-it-up},
  langid = {en}
For attribution, please cite this work as:
Michael DeWitt. 2018. “Speed It Up!” July 6, 2018. https://michaeldewittjr.com/programming/2018-07-06-speed-it-up.