Subcategory: None · Core

Don't Grow data.frame(s)

When you grow a data.frame by one, for example using rbind() or cbind() to add a row or column, R will allocate a new data.frame one larger and copy across all the old data. If you’re doing this regularly, that’s a lot of redundant expensive memory movement. Either pre-allocate your data.frame to the correct size at the start, or use a data.table instead which provides faster growth (and many other performance improvements).

Read More

Loop ordering

In Fortran, MATLAB, and R, arrays are stored in column-major order, meaning that the entries from the left-most index are stored contiguously in memory. This is often referred to as the left-most index varying quickest. This is different from many other popular scientific programming languages such as C, C++, and Python, which use a row-major ordering. The implication for writing loops is that it is always best to order loops by which indices vary quickest. In this way, the loop nest will traverse contiguous memory, which can be done efficiently.

Read More

Vectorisation

R has many functions which operate on entire vectors and matrices, which have been implemented in faster C. Vectorisation can perform mathematical operations many times faster than a for loop and even take advantage of conditional logic.

Read More