Skip to Content
Efficient R Programming
book

Efficient R Programming

by Colin Gillespie, Robin Lovelace
December 2016
Intermediate to advanced
222 pages
5h 20m
English
O'Reilly Media, Inc.
Content preview from Efficient R Programming

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

R Programming

R Programming

Jared P. Lander
R Programming By Example

R Programming By Example

Omar Trejo Navarro

Publisher Resources

ISBN: 9781491950777Errata PageSupplemental Content