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
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)){
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
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
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
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
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
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.
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
9 matches
Mail list logo