The multicore
package is deprecated: not parallel
. Take a look at the documentation for the mclapply
function: it's the easiest way to execute functions in parallel in the parallel
package. It's very similar to lapply
but with a few new, optional arguments:
library(parallel)
myfun <- function(i) { Sys.sleep(1); i }
mclapply(1:8, myfun, mc.cores=4)
Note that mclapply
uses processes, not threads, and doesn't support parallel execution on Windows. For Windows, you should take a look at parLapply
, which is also in parallel
. It is also similar to lapply
, but requires a cluster object as the first argument. Here's the same example, but this works on essentially any platform:
library(parallel)
cl <- makePSOCKcluster(4)
myfun <- function(i) { Sys.sleep(1); i }
parLapply(cl, 1:8, myfun)
stopCluster(cl)