I am using R parallel package to do parallel computation on my laptop:
> library(parallel)
> x = matrix(rep(1,2000), nrow=2)
> cl <- makeCluster(getOption("cl.cores", 8))
> system.time(replicate(5000, parApply(cl, x, 1, paste, collapse="-")))
user system elapsed
7.950 0.966 13.562
> stopCluster(cl)
> system.time(replicate(5000, apply(x, 1, paste, collapse="-")))
user system elapsed
8.357 0.001 8.355
Did I make any mistake here? The only thing I am not so sure about is how to use makeCluster
.
Update: to reduce the overhead cost of parallelization, use a much bigger matrix x
and remove replicate
in the benchmark; still, the different between parallel and serial are very marginal and sometimes parallel is slower.