Re: [R] “For” calculation is so slow

2012-05-23 Thread Petr PIKAL
I'm not sure what you are trying to prove with that example - the loopless versions are massively faster, no? In some languages loops are integral part of programming habits. In R you can many things do with whole objects without looping - vectorisation approach. See R-Inferno from Patrick

[R] “For” calculation is so slow

2012-05-22 Thread jiangxijixzy
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)){

Re: [R] “For” calculation is so slow

2012-05-22 Thread Liviu Andronic
On Tue, May 22, 2012 at 9:01 AM, jiangxijixzy jiangxiji...@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

Re: [R] “For” calculation is so slow

2012-05-22 Thread Petr PIKAL
Hi 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 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

Re: [R] “For” calculation is so slow

2012-05-22 Thread Zhou Fang
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

Re: [R] “For” calculation is so slow

2012-05-22 Thread Petr PIKAL
Hi For loops are really, really slow in R. In general, you want to avoid them I strongly disagree. ***Proper*** use of looping is quite convenient and reasonably fast. Consider system.time( { + a=0 + for (i in 1:1000) { + a -a+i + } + a + }) user system elapsed 10.220.02

Re: [R] “For” calculation is so slow

2012-05-22 Thread Petr PIKAL
And as followup system.time(d-1000*1001/2) user system elapsed 0.020.000.02 identical(a,b,d) [1] TRUE Regards Petr Hi For loops are really, really slow in R. In general, you want to avoid them I strongly disagree. ***Proper*** use of looping is quite

Re: [R] “For” calculation is so slow

2012-05-22 Thread Jeff Newmiller
Thanks, I will take that factor of 100 anytime rather than keep some syntactic familiarity from other languages. Not to say I don't ever use loops, but I always try to vectorize the inner loop, if not the inner two loops.

Re: [R] “For” calculation is so slow

2012-05-22 Thread Zhou Fang
I'm not sure what you are trying to prove with that example - the loopless versions are massively faster, no? I don't disagree that loops are sometimes unavoidable, and I suppose sometimes loops can be faster when the non-loop version e.g. breaks your memory budget, or performs tons of needless