作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试对两个向量进行 1000 次放回采样并计算均值比。重复此过程 10,000 次。
我编写了一个示例并行代码,但它比在一台机器上使用简单的 for 循环花费的时间要长得多。
ratio_sim_par <- function(x1, x2, nrep = 1000) {
# Initiate cluster
cl <- makeCluster(detectCores() - 1) #Leave one core for other operations
clusterExport(cl, varlist=c("x1", "x2", "nrep"), envir=environment())
Tboot <- parLapply(cl, 1:nrep, function(x){
n1 <- length(x1)
n2 <- length(x2)
xx1 <- sample(x1, n1, replace = TRUE) # sample of size n1 with replacement from x1
xx2 <- sample(x2, n2, replace = TRUE) # sample of size n2 with replacement from x2
return(mean(xx1) / mean(xx2))
})
stopCluster(cl)
return(unlist(Tboot))
}
ratio_sim_par(x1, x2, 10000)
系统时间受不了。谁能帮我理解我犯的错误?谢谢
最佳答案
将任务分配到不同的节点需要大量的计算开销,并且可以抵消您从并行化脚本中获得的任何 yield 。在您的情况下,您正在调用 parLapply
10,000 次并且可能花费比实际执行重采样更多的资源来 fork 每个任务。使用非并行版本的 ratio_sim_par
尝试这样的事情:
mclapply(1:10000, ratio_sim_par, x1, x2, nrep = 1000, mc.cores = n_cores)
mclapply
会将作业分成尽可能多的可用内核,并 fork 一次。我正在使用 mclapply
而不是 parLapply
因为我已经习惯了并且不需要那么多设置。
关于R 并行包 - 在我的玩具示例中性能非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61180092/
我不会说谎。这是一道作业题。但是,就我而言,积分已经消失了。现在,我只是在寻找答案,因为我-认为-我可能疯了。 这个程序的目标是执行命令ps -A | grep(输入字符串) | wc -l 以类似于
我有兴趣编写玩具 DVCS。 要研究的最小 DVCS 是什么? (我不需要 ssl、ssh、svn 兼容性等......它应该能够做本地存储库) 最佳答案 Git 的实际核心相当小。不幸的是,它不是用
近年来,低代码平台非常火,尤其是Mendix和Outsystems的收购及融资后,很多人越来越相信,低代码平台或将成为继云计算之后,又一个千亿级赛道。 什么是低代码平台 那么什么是低代码平台
我是一名优秀的程序员,十分优秀!