This chapter will suggest ways that you can enhance the performance of
your R code, taking into account the time/space trade-off.
14.1 Writing Fast R Code........................................................
What can be done to make R code faster? Here are the main tools available
to you:
- Optimize your R code through vectorization, use of byte-code compila-
tion, and other approaches. - Write the key, CPU-intensive parts of your code in a compiled language
such as C/C++. - Write your code in some form of parallel R.
The first approach will be covered in this chapter, and the other ap-
proaches are covered in Chapters 15 and 16.
To optimize your R code, you need to understand R’s functional pro-
gramming nature and the way R uses memory.
14.2 The Dreaded for Loop.......................................................
Ther-helpdiscussion listserv for R often has questions about how to accom-
plish various tasks withoutforloops. There seems to be a feeling that pro-
grammers should avoid these loops at all costs.^1 Those who pose the queries
usually have the goal of speeding up their code.
It’s important to understand that simply rewriting code to avoid loops
will not necessarily make the code faster. However, in some cases, dramatic
speedup may be attained, usually through vectorization.
14.2.1 Vectorization for Speedup.......................................
Sometimes, you can use vectorization instead of looping. For example, ifx
andyare vectors of equal lengths, you can write this:
z<-x+y
This is not only more compact, but even more important, it is faster
than using this loop:
for (i in 1:length(x)) z[i] <- x[i] + y[i]
Let’s do a quick timing comparison:
> x <- runif(1000000)
> y <- runif(1000000)
(^1) By contrast,whileloops pose much more of a challenge, because they are difficult to vectorize
effectively.
306 Chapter 14