Why are `colMeans()` and `rowMeans()` functions faster than using the mean function with `lapply()`?

人走茶凉 提交于 2019-12-01 06:24:51

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/12759937/why-are-colmeans-and-rowmeans-functions-faster-than-using-the-mean-funct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!