简体   繁体   English

使我的R代码运行更快(嵌套循环)

[英]Make my R code run faster (nested loops)

Imagine you have a 150 by 5 matrix. 假设您有一个150 x 5的矩阵。 Each element contains a random integer from 0 to 20. 每个元素包含一个从0到20的随机整数。

Now think each column of the matrix as independent; 现在认为矩阵的每一列都是独立的; I need to loop through all the possible combination of all 5 columns, which yields 150^5 = 75937500000 combinations. 我需要遍历所有5列的所有可能组合,得出150 ^ 5 = 75937500000组合。

It is critical I run every single combination exactly once. 至关重要的是,每个单一组合只能运行一次。 The order which I ran combinations do not matter. 我运行组合的顺序无关紧要。

I tried doing this using while loops. 我尝试使用while循环进行此操作。 See code below. 请参见下面的代码。

To run this loop, based on my calculation it would take me 54 hours on my laptop. 要运行此循环,根据我的计算,我需要花54个小时在笔记本电脑上。

Questions 问题

  • Any way to make my code run faster on my laptop? 有什么方法可以使我的代码在笔记本电脑上更快地运行? (bootstrapping?) (引导?)

  • If not, are there any web R servers I can access that would run my code at a significant faster rate? 如果不是,是否可以访问任何Web R服务器以更快的速度运行代码?

  • If not, would it make any significant difference to run this in another/faster language? 如果没有,以另一种/更快的语言来运行它会不会有重大的不同? (Python) (蟒蛇)

     while(counter1 <= 150) { while(counter2 <= 150) { while(counter3 <= 150) { while(counter4 <= 150) { while(counter5 <= 150) { #Other operations that take additional time# result<-c( giant_matrix[counter1,1], giant_matrix[counter2,2], giant_matrix[counter3,3], giant_matrix[counter4,4], giant_matrix[counter5,5]) counter5=counter5+1 } counter5=1 counter4=counter4+1 } counter4=1 counter3=counter3+1 } counter3=1 counter2=counter2+1 } counter2=1 counter1=counter1+1 } 

Here is the above solution with a 20 x 5 matrix of 100 elements. 这是具有100个元素的20 x 5矩阵的上述解决方案。 It results in a data frame of 3,200,000 x 5 size: 结果是一个3,200,000 x 5大小的数据帧:

m <- matrix(sample(1:20, 100, replace = TRUE), nrow = 20)
df <- expand.grid(m[, 1], m[, 2], m[, 3], m[, 4], m[, 5])

Example output of the above df (head): 上面df(头)的示例输出:

head(df)
  Var1 Var2 Var3 Var4 Var5
1   10   19   13    4    7
2   19   19   13    4    7
3    3   19   13    4    7
4    5   19   13    4    7
5   11   19   13    4    7
6    8   19   13    4    7

nrow(df)
[1] 3200000

dim(df)
[1] 3200000       5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM