简体   繁体   English

为模拟研究运行多次重复Rcpp函数的最有效方法?

[英]Most efficient way to run many repetitions of an Rcpp function for a simulation study?

I wrote an Rcpp function that returns a sample from a population, and to test the estimation method I'd like to run it thousands or millions of times. 我写了一个Rcpp函数,它从一个总体中返回一个样本,并测试我希望运行数千或数百万次的估计方法。 It seems that invoking Rcpp takes a little bit of overhead, and something like replicate(100,myFunction()) is taking much longer than I would expect. 似乎调用Rcpp需要一些开销,而像replicate(100,myFunction())的东西花费的时间比我想象的要长得多。

What's the best way to do this? 最好的方法是什么? rep , *apply , replicate , put the loop itself in C++? rep*applyreplicate ,把循环本身放在C ++中?

If you are concerned about performance you want to minimize the number of calls from R to C++ -- and not do it thousands or millions of times as you said. 如果你担心性能,你想减少从R到C ++的调用次数 - 而不是像你说的那样做数千或数百万次

So Baptiste is spot on: do the for() loop at the C++ level, if you can. 所以Baptiste就是如此:如果可以的话,在C ++级别执行for()循环。

Also note that the most recent version of RcppArmadillo now has a C++ version of sample() which may allow you to do that part in C++ too. 另请注意,最新版本的RcppArmadillo现在具有C ++版本的sample() ,这可能允许您在C ++中执行该部分。 That saidm I know nothing about "UPtille" and what it does so maybe you are stuck with the existing R implementation. 那说我对“UPtille”及其所做的一无所知,也许你会被现有的R实现所困扰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM