时间:2021-05-20
本文用于记录笔者在将R语言中的for语句并行化处理中的一些问题。
这里使用foreach和doParallel包提供的函数实现for语句的并行处理。
输出:
[1] "Result = 96.72, time = 0.177s"
输出:
[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"以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
R语言中的数据重塑是关于改变数据被组织成行和列的方式。大多数时间R语言中的数据处理是通过将输入数据作为数据帧来完成的。很容易从数据帧的行和列中提取数据,但是在某
在c语言中数字字符是字符类型,是按照字符串的处理方式处理字符,是占位符为%c。 字符指类字形单位或符号,包括字母、数字、运算符号、标点符号和其他符号,以及一些
既然了解了R语言的基本数据类型,那么如何将庞大的数据送入R语言进行处理呢?送入的数据又是如何在R语言中进行存储的呢?处理这些数据的方法又有那些呢?下面我们一起来
安装Pycharm进行Python开发时,经常右下角提示NoRinterpreterdefined,处理方式:1、安装R,然后将R的路径添加到Pycharm中:
前言在我学的这些编程语言中,总是记不住它们的时间处理方式,每次用到时都要重新看,所以想着在这里记录下来,也方便用到时查找,也方便有需要的朋友们参考。time_t