R語言中for循環的並行處理方式
前言
本文用於記錄筆者在將R語言中的for語句並行化處理中的一些問題。
實驗
這裡使用foreach和doParallel包提供的函數實現for語句的並行處理。
for語句腳本
func <- function(x, y, z) { return(x^y/z) } # >>> main <<< x <- 2 y <- 3 z <- 1:100000 start <- (proc.time())[3][[1]] a <- 0 for (i_z in z) { a <- a + func(x, y, i_z) } end <- (proc.time())[3][[1]] print(paste('Result = ', round(a, 2), ', time = ', (end-start), 's', sep=''))
輸出:
[1] “Result = 96.72, time = 0.177s”
並行化版本
library(foreach) library(doParallel) func <- function(x, y, z) { return(x^y/z) } # >>> main <<< x <- 2 y <- 3 z <- 1:100000 start <- (proc.time())[3][[1]] cl <- makeCluster(12) registerDoParallel(cl) a <- foreach(z=z, .combine='rbind') %dopar% func(x, y, z) a <- sum(a) stopCluster(cl) end <- (proc.time())[3][[1]] print(paste('Result = ', round(a, 2), ', time = ', (end-start), 's', sep=''))
輸出:
[1] “Result = 96.72, time = 37.988s”
總結
1、這裡發現並行化所用時間大於非並行化所用過的時間,是因為需要執行的操作(func函數)過於簡單,而foreach處理時會有額外的資源消耗。此時foreach額外消耗的資源遠大於需要執行的操作所需的資源,因此會導致並行化後反而使用的時間增加瞭。所以對於一些復雜的操作比較適合使用並行化的策略。
2、foreach函數的.packages參數可以為並行化函數傳遞額外需要的包。
3、foreach中的參數為需要在func中循環的變量,其他固定的變量則在func中傳入。參數可以是data.frame類型。
補充:R語言–for循環語句的使用
R語言for循壞語句的使用(多個for)
對於多個for循還語句,R語言的執行順序(以3個for為例):從外向內單個執行,裡邊循還完整,再往外一層,直到全部完成。話不多說,上例子:
代碼:
library(data.table) mm<-data.table() m<-c(1,2,3,4,5) n<-c('a','b','c','d','e') o<-c(6,7,8,9,10) for (i1 in m){ for ( i2 in n){ for (i3 in o){ print(c(i1,i2,i3)) aa<-data.table(i1,i2,i3) bb<-rbind(mm,aa) } } }
執行結果:
[1] "1" "a" "6" [1] "1" "a" "7" [1] "1" "a" "8" [1] "1" "a" "9" [1] "1" "a" "10" [1] "1" "b" "6" [1] "1" "b" "7" [1] "1" "b" "8" [1] "1" "b" "9" [1] "1" "b" "10" [1] "1" "c" "6" [1] "1" "c" "7" [1] "1" "c" "8" [1] "1" "c" "9" [1] "1" "c" "10" [1] "1" "d" "6" [1] "1" "d" "7" [1] "1" "d" "8" [1] "1" "d" "9" [1] "1" "d" "10" [1] "1" "e" "6" [1] "1" "e" "7" [1] "1" "e" "8" [1] "1" "e" "9" [1] "1" "e" "10" [1] "2" "a" "6" [1] "2" "a" "7" [1] "2" "a" "8" [1] "2" "a" "9" [1] "2" "a" "10" [1] "2" "b" "6" [1] "2" "b" "7" [1] "2" "b" "8" [1] "2" "b" "9" [1] "2" "b" "10" [1] "2" "c" "6" [1] "2" "c" "7" [1] "2" "c" "8" [1] "2" "c" "9" [1] "2" "c" "10" [1] "2" "d" "6" [1] "2" "d" "7" [1] "2" "d" "8" [1] "2" "d" "9" [1] "2" "d" "10" [1] "2" "e" "6" [1] "2" "e" "7" [1] "2" "e" "8" [1] "2" "e" "9" [1] "2" "e" "10" [1] "3" "a" "6" [1] "3" "a" "7" [1] "3" "a" "8" [1] "3" "a" "9" [1] "3" "a" "10" [1] "3" "b" "6" [1] "3" "b" "7" [1] "3" "b" "8" [1] "3" "b" "9" [1] "3" "b" "10" [1] "3" "c" "6" [1] "3" "c" "7" [1] "3" "c" "8" [1] "3" "c" "9" [1] "3" "c" "10" [1] "3" "d" "6" [1] "3" "d" "7" [1] "3" "d" "8" [1] "3" "d" "9" [1] "3" "d" "10" [1] "3" "e" "6" [1] "3" "e" "7" [1] "3" "e" "8" [1] "3" "e" "9" [1] "3" "e" "10" [1] "4" "a" "6" [1] "4" "a" "7" [1] "4" "a" "8" [1] "4" "a" "9" [1] "4" "a" "10" [1] "4" "b" "6" [1] "4" "b" "7" [1] "4" "b" "8" [1] "4" "b" "9" [1] "4" "b" "10" [1] "4" "c" "6" [1] "4" "c" "7" [1] "4" "c" "8" [1] "4" "c" "9" [1] "4" "c" "10" [1] "4" "d" "6" [1] "4" "d" "7" [1] "4" "d" "8" [1] "4" "d" "9" [1] "4" "d" "10" [1] "4" "e" "6" [1] "4" "e" "7" [1] "4" "e" "8" [1] "4" "e" "9" [1] "4" "e" "10" [1] "5" "a" "6" [1] "5" "a" "7" [1] "5" "a" "8" [1] "5" "a" "9" [1] "5" "a" "10" [1] "5" "b" "6" [1] "5" "b" "7" [1] "5" "b" "8" [1] "5" "b" "9" [1] "5" "b" "10" [1] "5" "c" "6" [1] "5" "c" "7" [1] "5" "c" "8" [1] "5" "c" "9" [1] "5" "c" "10" [1] "5" "d" "6" [1] "5" "d" "7" [1] "5" "d" "8" [1] "5" "d" "9" [1] "5" "d" "10" [1] "5" "e" "6" [1] "5" "e" "7" [1] "5" "e" "8" [1] "5" "e" "9" [1] "5" "e" "10"
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。
推薦閱讀:
- None Found