Chapter 7. Efficient Optimization
Donald Knuth is a legendary
American computer scientist who developed a number of the key algorithms
that we use today (see, for example, ?Random
). On the subject of
optimization, he gave this advice:
The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming.
Knuth’s point is that it is easy to undertake code optimization inefficiently. When developing code, the causes of inefficiencies may shift so that what originally caused slowness at the beginning of your work may not be relevant at a later stage. This means that time spent optimizing code early in the developmental stage could be wasted. Even worse, there is a trade-off between code speed and code readability; we’ve already made this trade-off once by using readable (but slow) R compared with verbose C code!
For this reason, this chapter is part of the latter half of the book. The previous chapters deliberately focused on concepts, packages, and functions to increase efficiency. These are (relatively) easy ways of saving time that, once implemented, will work for future projects. Code optimization, by contrast, is an advanced topic that should only be tackled once low hanging fruit for efficiency gains have been taken.
In this chapter we assume that you already have well-developed code that is mature conceptually and has been ...
Get Efficient R Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.