您好我正在尝试使用R包并行,特别是mclapply()函数使用我的8核系统生成超过10000列和24行的移动平均值(列上的移动平均值,因此24个数据点正在平滑,10000次)。
但是,当我将标准lapply的性能速度与mclapply进行比较时,我看不到加速。有人可以解释为什么我看到运行时间没有区别吗?
> x = replicate(10000, rnorm(24))
> system.time({
+ tmpp = lapply(list(x), function(x) {
+ rollmean(x, 7)
+ })
+ })
user system elapsed
15.309 7.893 23.201
>
> detectCores()
[1] 8
>
> system.time({
+ tmpp = mclapply(list(x),mc.cores=8, function(x) {
+ rollmean(x, 7)
+ })
+ })
user system elapsed
15.628 7.948 23.573
答案 0 :(得分:2)
您正在提供一个列表,因此没有并行性:
x <-replicate(10000, list(rnorm(24)) ) # list length 10000
library(zoo)
library(parallel)
system.time({
tmpp = lapply(x, function(x) {
rollmean(x, 7)
})
})
# user system elapsed
# 8.250 0.044 8.249
parallel::detectCores()
#[1] 8
system.time({
tmpp = mclapply(x, mc.cores=8, function(z) {
rollmean(z, 7)
})
})
# user system elapsed
# 14.376 8.858 3.922
(这是在6岁的MacPro上。)