Vectorization is the key difference between the two. I will rise to explain this. R is a computer language with a high level of interpretation. He takes care of many basic computer tasks for you. When you write
x <- 2.0
you do not need to tell the computer that
- "2.0" is a floating point number;
- "x" must store data with a numeric type;
- he must find a place in his memory in order to put "5";
- it should register "x" as a pointer to a specific place in memory.
R depicts these things separately.
But there is a price for such a comfortable problem: it is slower than low-level languages.
In C or FORTRAN, most of this βif testβ will be performed at compile time, rather than at run time. They are translated into binary computer language (0/1) after they are written, BUT before they start. This allows the compiler to organize binary machine code in an optimal way for interpreting the computer.
What does this have to do with vectorization in R? Well, many of the R functions are actually written in a compiled language such as C, C ++, and FORTRAN, and have a small R shell. That is the difference between your approach. For loops add additional test if operations that the machine must perform on the data, making it slower
source share