title: Using R parallel to speed up bootstrap
body: <p>I would like to speed up my bootstrap function, which works perfectly fine itself. I read that since R 2.14 there is a package called <code>parallel</code>, but I find it very hard for sb. with low knowledge of computer science to really implement it. Maybe somebody can help.</p>
  
  <p>So here we have a bootstrap:</p>
  
  <pre><code>n&lt;-1000
  boot&lt;-1000
  x&lt;-rnorm(n,0,1)
  y&lt;-rnorm(n,1+2*x,2)
  data&lt;-data.frame(x,y)
  boot_b&lt;-numeric()
  for(i in 1:boot){
    bootstrap_data&lt;-data[sample(nrow(data),nrow(data),replace=T),]
    boot_b[i]&lt;-lm(y~x,bootstrap_data)$coef[2]
    print(paste('Run',i,sep=" "))
  }
  </code></pre>
  
  <p>The goal is to use parallel processing / exploit the multiple cores of my PC. I am running R under Windows. Thanks!</p>
  
  <p><strong>EDIT (after reply by Noah)</strong></p>
  
  <p>The following syntax can be used for testing:</p>
  
  <pre><code>library(foreach)
  library(parallel)
  library(doParallel)
  registerDoParallel(cores=detectCores(all.tests=TRUE))
  n&lt;-1000
  boot&lt;-1000
  x&lt;-rnorm(n,0,1)
  y&lt;-rnorm(n,1+2*x,2)
  data&lt;-data.frame(x,y)
  start1&lt;-Sys.time()
  boot_b &lt;- foreach(i=1:boot, .combine=c) %dopar% {
    bootstrap_data&lt;-data[sample(nrow(data),nrow(data),replace=T),]
    unname(lm(y~x,bootstrap_data)$coef[2])
  }
  end1&lt;-Sys.time()
  boot_b&lt;-numeric()
  start2&lt;-Sys.time()
  for(i in 1:boot){
    bootstrap_data&lt;-data[sample(nrow(data),nrow(data),replace=T),]
    boot_b[i]&lt;-lm(y~x,bootstrap_data)$coef[2]
  }
  end2&lt;-Sys.time()
  start1-end1
  start2-end2
  as.numeric(start1-end1)/as.numeric(start2-end2)
  </code></pre>
  
  <p>However, on my machine the simple R code is quicker. Is this one of the known side effects of parallel processing, i.e. it causes overheads to fork the process which add to the time in 'simple tasks' like this one?</p>
  
  <p>Edit: On my machine the <code>parallel</code> code takes about 5 times longer than the 'simple' code. This factor apparently does not change as I increase the complexity of the task (e.g. increase <code>boot</code> or <code>n</code>). So maybe there is an issue with the code or my machine (Windows based processing?).</p>