One basic thing to do is to keep the R version up to date, since every update aims to make some processes more efficient. The version command returns a list that contains (among other things) the major and minor version of R currently being used. 
Is my code really slow? We need to compare the current solution with one or more alternatives. We just calculate the time each one takes to do the same, and we select the fastest.
Benchmarking:
Timing with system.time

Storing the result

Microbenchmark package

rds) using saveRDS(). To read in the rds file, we use readRDS(). 
How good is your machine
Of course the speed of an operation to be done depends on the power of the computer. You can compare the speed of your computer in doing some standard tasks with the Bechmarkme package, both in absoute time of such tasks and relatively to each other
R is flexible because you can often solve a single problem in many different ways. Some ways can be several orders of magnitude faster than the others. This chapter teaches you how to write fast base R code.
Memory allocation
Ex. 1 allocates the vector directly, 2 preallocates the vector, and 3 grows the vector
![]() | ![]() |
Importance of vectorizing your code
When we call a base R function, we eventually call a C or FORTRAN code, and that underlying code is very heavily optimised. So to make R code faster is to a access this optimised code as quickly as possible: This usually means vectorised code.
Vectorised functions. Many R functions are vectorised. Ex:

Data frames and matrices
![]() | ![]() |
The substantial difference is that data frames are multiple vectors while matrices are a single vector.
Profiling helps you locate the bottlenecks in your code (which part is the one that slows down the overall process). This chapter teaches you how to visualize the bottlenecks using the `profvis` package. 
profvis package
There are two ways of profiling with this package.


And the resulting output for the function call is the following, which gives the space and time required for each function of the code.

Ex. The monopoly simulation.
df <- data.frame(d1 = sample(1:6, 3, replace = TRUE), d2 = sample(1:6, 3, replace = TRUE))m <- matrix(sample(1:6, 6, replace = TRUE), ncol = 2)total <- apply(d, 1, sum)
In the previous exercise you switched the underlying object to a matrix. This makes the above apply operation three times faster. But there's one further optimization you can use - switch apply() with rowSums().& operator will always evaluate both its arguments. That is, if you type x & y, R will always try to work out what x and y are. The && operator takes advantage of this trick, and doesn't bother to calculate y if it doesn't make a difference to the overall result. One thing to note is that && only works on single logical values but & also works on vectors of length greater than 1. Some problems can be solved faster using multiple cores on your machine.




Parallel packages



We detect there is a correlation between attack and defense in our Pokemon dataset. First we create our bootstrap function
We have to run multiple times this function, and that's where parSapply enters.
