performance - Make my R code run faster (nested loops) -
imagine have 150 5 matrix. each element contains random integer 0 20.
now think each column of matrix independent; need loop through possible combination of 5 columns, yields 150^5 = 75937500000 combinations.
it critical run every single combination once. order ran combinations not matter.
i tried doing using while loops. see code below.
to run loop, based on calculation take me 54 hours on laptop.
questions
any way make code run faster on laptop? (bootstrapping?)
if not, there web r servers can access run code @ significant faster rate?
if not, make significant difference run in another/faster language? (python)
while(counter1 <= 150) { while(counter2 <= 150) { while(counter3 <= 150) { while(counter4 <= 150) { while(counter5 <= 150) { #other operations 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 above solution 20 x 5 matrix of 100 elements. results in data frame of 3,200,000 x 5 size:
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 above df (head):
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
Comments
Post a Comment