您好我正在研究并行计算的数据。 我希望看到每个k时间的总执行时间并存储到数据帧中。 但是当我看到结果存储在列表时间表中时,经过的时间是负值,这对我来说没有意义。我的代码出了什么问题?
library(doParallel)
library(doRNG)
cl <- makeCluster(3);
registerDoParallel(cl)
registerDoRNG(seed = 11)
tstart_par = proc.time()
time_chain_par = matrix(0, 10, 2)
timetable <- foreach(k=1:10, .options.RNG=11) %dorng%{
runif(10,0,1)
runif(10,0,1)
runif(10,0,1)
etime_par = proc.time() - tstart_par
}
> timetable
[[1]]
user system elapsed
-42.559 -8.004 -1483.762
[[2]]
user system elapsed
-43.179 -8.038 -1484.064
[[3]]
user system elapsed
-42.996 -8.065 -1484.305
[[4]]
user system elapsed
-42.558 -8.004 -1483.761
[[5]]
user system elapsed
-43.178 -8.038 -1484.063
[[6]]
user system elapsed
-42.558 -8.004 -1483.761
[[7]]
user system elapsed
-42.996 -8.064 -1484.304
[[8]]
user system elapsed
-43.178 -8.038 -1484.062
[[9]]
user system elapsed
-42.558 -8.004 -1483.760
[[10]]
user system elapsed
-43.177 -8.038 -1484.061
答案 0 :(得分:1)
proc.time()
在群集中重置,因此您要比较主R进程运行的时长与新创建的群集运行的时间,并给出负值。在这种情况下最好使用Sys.time()
函数,它比较中央时钟。因此,要计算从启动并行流程到每个群集完成处理的时间:
library(doParallel)
library(doRNG)
cl <- makeCluster(3);
registerDoParallel(cl)
registerDoRNG(seed = 11)
tstart_par = Sys.time()
timetable <- foreach(k=1:10, .options.RNG=11) %dorng%{
runif(10,0,1)
runif(10,0,1)
runif(10,0,1)
etime_par = Sys.time() - tstart_par
}
stopCluster(cl)
> timetable
[[1]]
Time difference of 2.897783 secs
[[2]]
Time difference of 2.897755 secs
[[3]]
Time difference of 2.899669 secs
[[4]]
Time difference of 2.899365 secs
[[5]]
Time difference of 2.899258 secs
[[6]]
Time difference of 2.900039 secs
[[7]]
Time difference of 2.901185 secs
[[8]]
Time difference of 2.901083 secs
[[9]]
Time difference of 2.901798 secs
[[10]]
Time difference of 2.902622 secs
或者每个单独的集群运行多长时间你可以使用proc.time(),但是把它放在函数内部(我增加了样本数量,这样它可以运行更长时间,你可以看到差异):
cl <- makeCluster(3);
registerDoParallel(cl)
registerDoRNG(seed = 11)
time_chain_par = matrix(0, 10, 2)
timetable <- foreach(k=1:10, .options.RNG=11) %dorng%{
tstart_par = proc.time()
runif(1000000,0,1)
etime_par = proc.time() - tstart_par
}
stopCluster(cl)
timetable
[[1]]
user system elapsed
0.148 0.008 0.307
[[2]]
user system elapsed
0.145 0.008 0.249
[[3]]
user system elapsed
0.145 0.008 0.325
[[4]]
user system elapsed
0.062 0.002 0.130
[[5]]
user system elapsed
0.062 0.002 0.118
[[6]]
user system elapsed
0.061 0.003 0.110
[[7]]
user system elapsed
0.062 0.002 0.110
[[8]]
user system elapsed
0.061 0.003 0.149
[[9]]
user system elapsed
0.062 0.002 0.117
[[10]]
user system elapsed
0.097 0.003 0.169