我正在尝试运行%dopar%的小测试,它总是变慢。这里是一些虚拟代码和输出。我在win7x64核心i7-2600k上运行它...
library(foreach)
library(doSNOW)
registerDoSNOW(makeCluster(3, type = "SOCK"))
N <- 3*(10^4)
system.time(foreach(i = 1:N) %do% {sum(rnorm(N))})
system.time(foreach(i = 1:N) %dopar% {sum(rnorm(N))} )
这是输出:
> system.time(foreach(i = 1:N) %do% {sum(rnorm(N))})
user system elapsed
90.39 0.00 90.42
> system.time(foreach(i = 1:N) %dopar% {sum(rnorm(N))} )
user system elapsed
17.00 0.89 177.11