服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - R语言 - R语言中for循环的并行处理方式

R语言中for循环的并行处理方式

2022-01-11 15:33SANGF R语言

这篇文章主要介绍了R语言中for循环的并行处理方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

前言

本文用于记录笔者在将R语言中的for语句并行化处理中的一些问题。

实验

这里使用foreach和doParallel包提供的函数实现for语句的并行处理。

for语句脚本

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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"

并行化版本

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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为例):从外向内单个执行,里边循还完整,再往外一层,直到全部完成。话不多说,上例子:

代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
[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"

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://www.cnblogs.com/sangf/p/14336269.html

延伸 · 阅读

精彩推荐
  • R语言R语言可视化存储矢量图实现方式

    R语言可视化存储矢量图实现方式

    这篇文章主要为大家介绍了R语言存储矢量图的实现方式过程,有需要的朋友可以借鉴参考下,希望能够有所你帮助,祝大家多多进步,早日升职加薪...

    Kanny广小隶9392022-01-20
  • R语言R语言创建矩阵的实现方法

    R语言创建矩阵的实现方法

    这篇文章主要介绍了R语言创建矩阵的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随...

    hjk_caesar9462021-12-27
  • R语言R语言-使用ifelse进行数据分组

    R语言-使用ifelse进行数据分组

    这篇文章主要介绍了R语言-使用ifelse进行数据分组,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    小力丸12042022-01-10
  • R语言聊聊R语言中Legend 函数的参数用法

    聊聊R语言中Legend 函数的参数用法

    这篇文章主要介绍了聊聊R语言中Legend 函数的参数用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    Gavin姓陈6352021-12-28
  • R语言R语言编程学习绘制动态图实现示例

    R语言编程学习绘制动态图实现示例

    这篇文章主要介绍了R语言编程学习绘制动态图实现示例,有需要的的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    微小冷7652022-01-18
  • R语言R studio 批量注释的快捷方式

    R studio 批量注释的快捷方式

    这篇文章主要介绍了R studio 批量注释的快捷方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    JasonKQLin13342022-01-04
  • R语言R语言绘制散点图实例分析

    R语言绘制散点图实例分析

    在本篇文章里小编给大家整理的是一篇关于R语言绘制散点图实例分析内容,有需要的朋友们可以学习下。...

    w3cschool6942022-01-06
  • R语言R语言中c()函数与paste()函数的区别说明

    R语言中c()函数与paste()函数的区别说明

    这篇文章主要介绍了R语言中c()函数与paste()函数的区别说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    yiyu_j11152022-01-12