[R] sapply and loop

Thomas Lumley tlumley at u.washington.edu
Mon Oct 18 18:14:49 CEST 2004


On Sat, 16 Oct 2004, Zhen Pang wrote:

> Dear all,
>
> I am doing 200 times simulation. For each time, I generate a matrix and 
> define some function on this matrix to get a 6 dimension vector as my 
> results.
>
> As the loop should be slow, I generate 200 matrice first, and save them into 
> a list named ma,
> then I define zz<-sapply(ma, myfunction)
>
> To my surprise, It almost costs me the same time to get my results if I 
> directly use a loop from 1 to 200. Is it common? Can I improve any further?
>

It is quite common for a loop to be as fast as sapply(). After all, 
sapply() still has to run `myfunction' 200 times, and this is what takes 
most of the time, so there isn't any obvious reason why sapply() should be 
much faster.  sapply() and lapply() certainly can be faster than loops, 
but usually not by very much.

The surprising fact is that so many people *believe* the apply() functions 
are typically much faster than loops. It's probably a useful belief, since 
it encourages people to learn to use them (and they sometimes are faster).

You should try using Rprof() to find out which parts of your code are 
slow.

 	-thomas




More information about the R-help mailing list