I've been learning R for a while now, and have come across a lot of advice to programming types like myself to vectorize operations. Being a programmer, I'm interested as to why / how it's faster. An example:
n = 10^7
# populate with random nos
v=runif(n)
system.time({vv<-v*v; m<-mean(vv)}); m
system.time({for(i in 1:length(v)) { vv[i]<-v[i]*v[i] }; m<-mean(vv)}); m
This gave
   user  system elapsed 
   0.04    0.01    0.07 
[1] 0.3332091
   user  system elapsed 
  36.68    0.02   36.69 
[1] 0.3332091
The most obvious thing to consider is that we're running native code, i.e. machine code compiled from C or C++, rather than interpreted code, as shown by the massive difference in user time between the two examples (circa 3 orders of magnitude). But is there anything else going on? For example, does R do:
- Cunning native data structures, e.g. clever ways of storing sparse vectors or matrices so that we only do multiplications when we need to? 
- Lazy evaluation, e.g. on a matrix multiply, don't evaluate cells until as and when you need to. 
- Parallel processing. 
- Something else. 
To test whether there might be some sparse vector optimization I tried doing dot products with difference vector contents
# populate with random nos
v<-runif(n)
system.time({m<-v%*%v/n}); m
# populate with runs of 1 followed by 99 0s
v <-rep(rep(c(1,rep(0,99)),n/100))
system.time({m<-v%*%v/n}); m
# populate with 0s
v <-rep(0,n)
system.time({m<-v%*%v/n}); m
However there was no significant difference in time (circa 0.09 elapsed)
(Similar question for Matlab: Why does vectorized code run faster than for loops in MATLAB?)
 
     
     
     
    