Is there performance difference between NumericVector and vector<double>?

筅森魡賤 提交于 2019-12-23 14:06:29

问题


Suppose one uses NumericVector and the other uses vector<double> in their Rcpp code. Is there notable difference between the two usages, especially in performance?


回答1:


Generally, yes.

All of the Rcpp(11) types are "thin proxy objects" (which we talk about in several places, talks, slide decks, my book, ...) around the underlying SEXP objects. That means no copies are made when you go from R to C++, and when you go back from C++ to R.

Using standard C++ types like std::vector<T>, however, generally requires a copy.

So you should easily see a difference on some trivial test script as N increases enough.

Personally speaking, I generally like the "clean" use of C++ / STL types for code that "feels more C++-ish" but remain aware of the performance penalty. Often it does not really matter as the C++ solution is faster than what you replace in a pure R solution.

But your question is if one dominates the other, and the other is a clear yes.



来源:https://stackoverflow.com/questions/27031783/is-there-performance-difference-between-numericvector-and-vectordouble

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