20 juillet 2018

What are the architectural differences between R and C++?

From source code to binary

  • High level language : the statistician understands and writes "english".
  • Low level language: the CPU reads and runs binary (or machine) codes only.

From source code to binary

We need a translator (compiler or interpreter) to go from source to binary code.

Compiler versus interpreter

The Compiler The Interpreter
Input takes the entire program and translates it as a whole into machine code. takes one statement at a time.
Workload runs once and only needs to be called again for re-translation. runs each time the code needs to be executed.
Errors generates the error message only after scanning the whole program: debugging is hard. continues translating the program until the first error is met, in which case it stops: debugging is easy.
Sharing harder to share: binary may not be read by different OS, source code must be re-compiled. easy to share: just pass the source code.

Compiled language: memory allocation

Because at "compile time", the compiler needs to know exactly what objects are, use of variables is less flexible:

int x;      // Construction: allocate 
            // memory to the object.
            
x = 15;     // Initialisation of 
            // the object.
            
x = x + 1;  // Do stuff.

~x;         // Destruction: clear memory. 
            // Usually implicit.

How Rcpp goes from C++ to R