Replacing a function call with the body of the called function is called “inline expansion”. This eliminates the function calling overhead and also the overhead of return call from a function. It also saves the overhead of variables push/pop on the stack while function calling.
As a general rule of thumb, in any programming language, we should undertake memory management as much as possible. When we grow a vector inside a loop, the vector asks the processor for extra space in between the running program and then proceeds, once it gets the required memory. This process is repeated for every iteration of the loop. Thus we should pre-allocate the required memory to a vector to avoid such delays.
A golden rule in R programming is to access the underlying C/Fortran routines as much as possible; the fewer R function calls required to achieve this, the better. Many R functions are therefore vectorized, that is, the function’s inputs and/or outputs naturally work with vectors, reducing the number of function calls required.
The idea would be to replace the different one-column extraction
alternatives by the much faster .subset2
call
alternative.
For some R classes, the [[ ]]
operator and
.subset
work differently. For instance, they seem to be
equivalent for data.frame
but are not the same for
matrix
class.
Moreover, both [[ ]]
and .subset2
are
functions and in R, any function can be overwritten. Thus the above
optimization can be made to fail just by redefining, say, the the
.subset2
function.
The idea would be to replace the different one-value extraction
alternatives by the much faster .subset2
call
alternative.
For some R classes, the [[ ]]
operator and
.subset
work differently. For instance, they seem to be
equivalent for data.frame
but are not the same for
matrix
class.
Moreover, both [[ ]]
and .subset2
are
functions and in R, any function can be overwritten. Thus the above
optimization can be made to fail just by redefining, say, the
.subset2
function.