gpt4 book ai didi

Rcpp rowMaxs 与 matrixStats rowMaxs

转载 作者:行者123 更新时间:2023-12-03 16:49:15 26 4
gpt4 key购买 nike

我正在尝试在 Rcpp 中有效地计算 rowMaxs。一个非常简单的实现是

arma::mat RcppRowmaxs(arma::mat x){  

int N = x.n_rows;
arma::mat rm(N,1);

for(int nn = 0; nn < N; nn++){
rm(nn) = max(x.row(nn));
}

return(rm);
}

这工作得很好。但是,将此功能与其他包进行比较,结果证明其他实现要高效得多。具体来说, Rfast::rowMaxs比简单的 Rcpp 实现快 6 倍以上!

当然,我试图模仿 Rfast 的行为。 .
但是,作为Rcpp的初学者,我只尝试加载 Rfast::rowMaxs直接在 Rcpp 中描述,例如 here .不幸的是,根据我的基准测试,使用 Rcpp 脚本加载再次调用 Rcpp 脚本的 R 函数似乎很慢(请参阅“RfastinRcpp”行):
m = matrix(rnorm(1000*1000),1000,1000)

microbenchmark::microbenchmark(

matrixStats = matrixStats::rowMaxs(m),
Rfast = Rfast::rowMaxs(m,value=T),
Rcpp = RcppRowmaxs(m),
RfastinRcpp = RfastRcpp(m),
apply = apply(m,1,max)

)

Unit: microseconds
expr min lq mean median uq max neval cld
matrixStats 1929.570 2042.8975 2232.1980 2086.5180 2175.470 4025.923 100 a
Rfast 666.711 727.2245 842.5578 795.2215 891.443 1477.969 100 a
Rcpp 5552.216 5825.4855 6186.9850 5997.8295 6373.737 8568.878 100 b
RfastinRcpp 7495.042 7931.2480 9471.8453 8382.6350 10659.672 19968.817 100 b
apply 12281.758 15145.7495 22015.2798 17202.9730 20310.939 136844.591 100 c

关于如何提高我上面提供的功能的性能的任何提示?我查看了来自 Rfast 的源代码并相信 this是正确的文件。但是,到目前为止,我还没有找到代码的重要部分。

编辑:将帖子更改为关注 Rfast现在,按照 Michail 的回答。

最佳答案

我刚刚在我的笔记本电脑上做了一些实验。我有一个 5 岁的 HP,有 2 个 2.3 GHz 的英特尔 i5 内核。附上一张我的结果的图片。 Rfast 的实现比 matrixStats 的实现快得多,而且随着矩阵变大,时间差也会增加。

enter image description here

关于Rcpp rowMaxs 与 matrixStats rowMaxs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61183243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com