Dear All, The function I wrote can run well with the small data, but with the large data, the function runs very very slowly. How can I correct it? Thank you very much. My function as below: a<-c(1:240) b<-c(1:240) l=function(a,b){ v=0 u=0 uv=0 v[1]=0 u[1]=0 uv[1]=0 for (i in 1:(length(s)-1)){ v[i]<-((gx[[i]][b,(gx[[i]][a,1]+1)])-(gx[[i]][a,gx[[i]][a,1]+1]))/(gx[[i]][a,gx[[i]][a,1]+1]) u[i]<-((gy[[i]][a,(gy[[i]][a,1]+1)])-(gy[[i]][b,gy[[i]][a,1]+1]))/(gy[[i]][a,gy[[i]][a,1]+1]) uv[i]<-v[i]+u[i] } w=0 w=mean(uv) } kk<-data.frame() for (a in 1:240){ for (b in 1:240){ if (a<b) kk[a,b]=l(a,b) else kk[a,b]=0 }} max(kk) -- View this message in context: http://r.789695.n4.nabble.com/For-calculation-is-so-slow-tp4630830.html Sent from the R help mailing list archive at Nabble.com.
Hello, 's' is missing in your code. Regards ----- Mail original ----- De?: jiangxijixzy <jiangxijixzy at 163.com> ??: r-help at r-project.org Cc?: Envoy? le : Mardi 22 mai 2012 16h01 Objet?: [R] ?For? calculation is so slow Dear All, The function I wrote can run well with the small data, but with the large data, the function runs very very slowly. How can I correct it? Thank you very much. My function as below: a<-c(1:240) b<-c(1:240) l=function(a,b){ v=0 u=0 uv=0 v[1]=0 u[1]=0 uv[1]=0 for (i in 1:(length(s)-1)){ v[i]<-((gx[[i]][b,(gx[[i]][a,1]+1)])-(gx[[i]][a,gx[[i]][a,1]+1]))/(gx[[i]][a,gx[[i]][a,1]+1]) u[i]<-((gy[[i]][a,(gy[[i]][a,1]+1)])-(gy[[i]][b,gy[[i]][a,1]+1]))/(gy[[i]][a,gy[[i]][a,1]+1]) uv[i]<-v[i]+u[i] } w=0 w=mean(uv) } kk<-data.frame() for (a in 1:240){ for (b in 1:240){ if (a<b) kk[a,b]=l(a,b) else kk[a,b]=0 }} max(kk) -- View this message in context: http://r.789695.n4.nabble.com/For-calculation-is-so-slow-tp4630830.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On Tue, May 22, 2012 at 9:01 AM, jiangxijixzy <jiangxijixzy at 163.com> wrote:> The function I wrote can run well with the small data, but with the large > data, the function runs very very slowly. How can I correct it? Thank you > very much. My function as below: >I guess this is a classic loops vs vectorization problem.> fortune('treat')Contrary to popular belief the speed of R's interpreter is rarely the limiting factor to R's speed. People treating R like C is typically the limiting factor. You have vector operations, USE THEM. -- Byron Ellis R-help (October 2005) I would suggest that you read up on vectorization in R to understand why it is often preferred to loops. Two obvious places are an Rnews article by John Fox (if I remember well) and his Companion to Applied Regression book. Moreover, please sign your messages with your real name. Regards Liviu
For loops are really, really slow in R. In general, you want to avoid them like the plague. If you absolutely must insist on using them in large, computationally intense and complex code, consider implementing the relevant parts in C, say, and calling that from R. Staying within R, you can probably considerably speed up that code by storing gx and gy as a multi-dimensional arrays. (e.g. for sample data, something like rawGy = sample( 1:240, 240^2* 241, replace = T) rawGx = sample( 1:240, 240^2 *241, replace = T) gx = array(rawGx, dim = c(length(s) - 1, 240, max(rawGx)+1 ) ) gy = array(rawGy, dim = c(length(s) - 1, 240, max(rawGy)+1 ) ) ), in which case, you can easily do the computation without loops by gxa = (gx[ ,a,1]+ 1) gya =(gy[ ,a, 1] +1) uv = gx[cbind(1:(length(s) - 1) , b, gxa)] / gx[cbind(1:(length(s) - 1) , a, gxa)] - gy[cbind(1:(length(s) - 1) ,b, gya)]/gy[cbind(1:(length(s) - 1) ,a, gya)] or similar, which will be enormously faster (on my computer, there's an over 30x speed up). With a bit of thought, I'm sure you can also figure out how to let it vectorise in a, as well... Zhou -- View this message in context: http://r.789695.n4.nabble.com/For-calculation-is-so-slow-tp4630830p4630855.html Sent from the R help mailing list archive at Nabble.com.
Hi> > Dear All, > > The function I wrote can run well with the small data, but with thelarge> data, the function runs very very slowly. How can I correct it? Thankyou Your function does not run slowly, it does not run at all.> l(10,20)Error in l(10, 20) : object 's' not found No s object found. I presume that also gx and gy will not be found. And besides I am pretty sure that you probably does not need any loop at all.> very much. My function as below: > > a<-c(1:240) > b<-c(1:240) > l=function(a,b){ > v=0 > u=0 > uv=0 > v[1]=0Why? v<-0 v [1] 0 v[1]<-0 v [1] 0 So there is no difference between first "v" and second "v"> u[1]=0 > uv[1]=0 > for (i in 1:(length(s)-1)){ >v[i]<-((gx[[i]][b,(gx[[i]][a,1]+1)])-(gx[[i]][a,gx[[i]][a,1]+1]))/(gx[[i]]> [a,gx[[i]][a,1]+1]) >u[i]<-((gy[[i]][a,(gy[[i]][a,1]+1)])-(gy[[i]][b,gy[[i]][a,1]+1]))/(gy[[i]]> [a,gy[[i]][a,1]+1]) > uv[i]<-v[i]+u[i] > } > w=0 > w=mean(uv)Your function does not return anything. Here you can test it by yourself on shortened version l=function(a,b){ v=0 u=0 uv=0 v<-10 u<-20 uv<-v+u w=0 w=mean(uv) } Regards Petr> } > kk<-data.frame() > for (a in 1:240){ > for (b in 1:240){ > if (a<b) > kk[a,b]=l(a,b) > else kk[a,b]=0 > }} > max(kk) > > > -- > View this message in context: http://r.789695.n4.nabble.com/For- > calculation-is-so-slow-tp4630830.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.