What I want to ask is, algorithmically, what do the rowMeans() and colMeans() functions do to optimize speed?
Why are `colMeans()` and `rowMeans()` functions faster than using the mean function with `lapply()`?
Asked
Active
Viewed 1,412 times
3
Paul Hiemstra
- 58,502
- 12
- 138
- 147
user1482678
- 123
- 1
- 6
-
9I don't think it's algorithmic, I think it's a matter of what can be coded directly in C and what has to go through the R interpreter. – Ben Bolker Oct 06 '12 at 13:05
2 Answers
5
In addition, consider what lapply() does. It sets up repeated calls to the function mean(). So as well as the overhead of actually computing a mean (which is done in fast C code), the lapply() version repeatedly incurs the overhead of the sanity checking code and method dispatch associated with mean().
rowMeans() and colMeans() incur only a single set of sanity checks as internally, their C code is optimised to loop over the rows/columns there rather than via separate R calls.
Gavin Simpson
- 164,190
- 25
- 377
- 440
4
rowMeans and colMeans are faster than because they call C code directly, rather than being interpreted by the R interpreter.
Andrie
- 170,733
- 42
- 434
- 486
-
1Is there a difference in speed/efficiency between ``rowMeans`` and ``colMeans``? Thanks. (looked at various questions on so, but couldn't find it mentioned, did I miss a related post on this?) – PatrickT Oct 25 '17 at 19:00