网上有人说foreach包可以并行,我就去弄了,结果发现一个普通的二重循环什么事都不错都很卡!捣鼓了半天才发现是foreach的问题
为了提速,做了如下事宜:
- 直接利用矩阵列加减,不是一个个遍历加
- 把loop里面的函数调用去掉(开销很大)
- 不使用foreach,使用原始的for
#计算trustl R.trust_matrix <- matrix(rep(0,R.item_count*R.user_count),nrow=R.user_count, ncol=R.item_count); to_add = matrix(rep(1,R.user_count),nrow=R.user_count,ncol=1) for(i in 1:R.user_count) { for(j in 1:R.item_count) { if(abs( R.init_matrix[i,j] - R.init_predict_matrix[i,j] < error_threshold)){ R.trust_matrix[,j] <- R.trust_matrix[,j] + to_add R.trust_matrix[i,j] <- R.trust_matrix[i,j] - 1 } } print(i) }
这里记录了利用多核计算的一些包,有时间再琢磨:
http://cran.r-project.org/web/views/HighPerformanceComputing.html
这里有一个foreach的用法
http://cran.r-project.org/web/packages/foreach/vignettes/foreach.pdf