Summary
In this chapter, we covered a variety of techniques to leverage code in compiled languages for a near-native CPU performance. While the examples focused on C and C++, similar approaches can be used with Fortran or Java.
We first saw how compiling an R code before its execution by using the compile
package can provide modest performance gains, especially for code with many loops and basic operations. JIT compilation does the same automatically for executing any R code. There is a limit, however, to how much an R code can be optimized because R is a dynamic language at its core.
Looking beyond R, we used C and C++ to achieve dramatic performance improvements. We learned how to define a C function from within R, using the inline
package, and how to use Rcpp
to call an external C++ function from R.
In the process, we learned about how R represents different types of data in C/C++ using SEXP
pointers and SEXPREC
record structures, and how to manipulate R objects using these constructs....