4

What I want to ask is, algorithmically, what do the rowMeans() and colMeans() functions do to optimize speed?

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
user1482678
  • 133
  • 1
  • 6
  • 9
    I 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 Answers2

6

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
  • 170,508
  • 25
  • 396
  • 453
4

rowMeans and colMeans are faster than because they call C code directly, rather than being interpreted by the R interpreter.

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • 1
    Is 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