title: R parallel loops
body: <p>I am trying to start to parallelize workload in R. As I am only approaching this new way of writing code, I have done some benchmark to understand how effective this could be.</p>
  
  <p>Here there is the code:</p>
  
  <pre><code>library(doParallel)  
  library(doSNOW)
  library(foreach)
  library(tictoc)
  
  no_cores &lt;- detectCores() - 1  
  
  cl &lt;- makeCluster(no_cores)  
  registerDoParallel(cl)
  
  tic()
  result &lt;- foreach(i=1:10000,.combine=c) %dopar%{
  i^2
  } 
  toc()
  
  stopCluster(cl)
  
  tic()
  result&lt;-c()
  for (i in 1:10000){
  result[i]&lt;-i^2
  }
  toc()
  </code></pre>
  
  <p>and here the results:</p>
  
  <pre><code>&gt; library(doParallel)  
  &gt; library(doSNOW)
  &gt; library(foreach)
  &gt; library(tictoc)
  
  &gt; no_cores &lt;- detectCores() - 1  
  &gt; 
  &gt; cl &lt;- makeCluster(no_cores)  
  &gt; registerDoParallel(cl)
  &gt; 
  &gt; tic()
  &gt; result &lt;- foreach(i=1:10000,.combine=c) %dopar%{
  +   i^2
  + } 
  &gt; toc()
  3.83 sec elapsed
  &gt; 
  &gt; stopCluster(cl)
  &gt; 
  &gt; 
  &gt; tic()
  &gt; result&lt;-c()
  &gt; for (i in 1:10000){
  +   result[i]&lt;-i^2
  + }
  &gt; toc()
  &gt; 0.02 sec elapsed
  </code></pre>
  
  <p>It looks like that the serial execution took less time. Is there anything wrong with my approach?</p>