您好,登錄后才能下訂單哦!
怎么在R語言中使用for循環實現并行處理?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
R語言是用于統計分析、繪圖的語言和操作環境,屬于GNU系統的一個自由、免費、源代碼開放的軟件,它是一個用于統計計算和統計制圖的優秀工具。
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循環語句的使用
對于多個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"
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。