[R] for loop Vs apply function Vs foreach (REvolution enhancement)

julien cuisinier j_cuisinier at hotmail.com
Tue Feb 16 12:25:40 CET 2010


Dear all,


I know this topic has already been covered in other posts (at least the for loop Vs apply family of function), but I am looking for fresh / up-to-date opinion and feedback on those 3 methods to run unavoidable loops in R. I realise that it may be too general question for many, so any feedback appreciated.

1. apply Vs for loop

>> Seems apply is (was?) supposed to be faster than using for loop, some posts mention that it is now more of a cosmetic function (wrapper for "for loop") making the code essentially neater. Any thoughts/opinion/experience on this more than welcome. 

>> Running the very simple function attached, I end up with for loop quicker than apply function....but may be do I not use the apply function properly? 

2. foreach (REvolution enhancement)

>> seems the rationale of this function is to facilitate the use of multithreading to enhance the for loop speed. Given a moderate time sensitivity (process must run fast but a gain of 10-20% speed seen as probably not justifying the additional learning + dependence from yet another package), is it really worth going down that route? 

Has anyone extensive experience with this matter (using foreach to boost for loop running time)? any feedback welcome.


Have all a nice day!


Rgds,
Julien
 		 	   		  
_________________________________________________________________
Hotmail: Vertrauenswürdige E-Mails dank leistungsstarkem SPAM-Schutz.



More information about the R-help mailing list