John Sorkin
2006-May-14 14:55 UTC
[R] apply, apply speed vs. traditional looping mechanisms
Can someone tell me why apply (and apply) are faster in performing repeated operations than a for (or do) loop? I am looking for a technical explanation. Thanks, John John Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics Baltimore VA Medical Center GRECC and University of Maryland School of Medicine Claude Pepper OAIC University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 410-605-7119 jsorkin at grecc.umaryland.edu
Prof Brian Ripley
2006-May-14 20:26 UTC
[R] apply, apply speed vs. traditional looping mechanisms
On Sun, 14 May 2006, John Sorkin wrote:> Can someone tell me why apply (and apply) are faster in performing > repeated operations than a for (or do) loop? I am looking for a > technical explanation.apply() is just a wrapper for a for loop. So it is not faster that at least one implementation using a for loop: it may be neater and easier to understand than an explicit for loop. I don't understand why you used 'apply' twice here. lapply() can be faster than a carefully crafted for() loop (since C-level code is more efficient in memory allocation), but its main rationale is clarity (and especially to avoid traps like ans <- vector("list", n) for(i in 1:n) ans[[i]] <- fun(i) if n turns out to be zero or fun(i) to be NULL). -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
John Sorkin
2006-May-14 20:36 UTC
[R] apply, apply speed vs. traditional looping mechanisms
Prof Ripley, Many thanks for your reply. I did not mean to use apply twice, I meant to ask about apply and lapply. I understand your comment re: apply. I assume your comment about lapply is meant to mean that lapply is implemented in C code and therefore should be faster than a loop written in R. I would also like to take this opportunity to thank you for your may contributions to R and this list. Thank you, John Sorkin John Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics Baltimore VA Medical Center GRECC and University of Maryland School of Medicine Claude Pepper OAIC University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 410-605-7119 jsorkin at grecc.umaryland.edu>>> Prof Brian Ripley <ripley at stats.ox.ac.uk> 05/14/06 4:26 PM >>>On Sun, 14 May 2006, John Sorkin wrote:> Can someone tell me why apply (and apply) are faster in performing > repeated operations than a for (or do) loop? I am looking for a > technical explanation.apply() is just a wrapper for a for loop. So it is not faster that at least one implementation using a for loop: it may be neater and easier to understand than an explicit for loop. I don't understand why you used 'apply' twice here. lapply() can be faster than a carefully crafted for() loop (since C-level code is more efficient in memory allocation), but its main rationale is clarity (and especially to avoid traps like ans <- vector("list", n) for(i in 1:n) ans[[i]] <- fun(i) if n turns out to be zero or fun(i) to be NULL). -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595