Using compiled languages in R
Code compilation can provide modest gains in computational performance, but there are limits to these gains because the compiled code still needs to be evaluated by R in a dynamic fashion. For example, we explained in Chapter 3, Simple Tweaks to Make R Run Faster, how R, being a dynamically typed language, needs to check the type of an object before applying any operations. In the case of mov.avg()
, every time R encounters the +
operator, it needs to check that x
is a numeric vector, as it could have been modified between each iteration of the for
loop. In contrast, a statically typed language performs these checks at compile time, resulting in much faster run time performance.
For this and many other reasons, R's dynamic nature poses barriers to computational performance. The only way to break through these barriers is to turn to compiled languages such as C and use them from within R. This section assumes that you have some basic knowledge of compiled...