NumericVector和vector< double>之间是否存在性能差异?
假设其中一个在其Rcpp代码中使用NumericVector
,而另一个使用vector<double>
.两种用法之间是否有显着差异,尤其是在性能上?
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?
通常,是的.
所有Rcpp(11)类型都是围绕基础SEXP
对象的瘦代理对象"(我们在几个地方都在谈论,讨论,幻灯片,我的书等等).这意味着当您从R转到C ++以及从C ++回到R时不会复制任何副本.
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.
但是,使用标准的C ++类型(例如std::vector<T>
)通常需要一个副本.
Using standard C++ types like std::vector<T>
, however, generally requires a copy.
因此,随着N
增加得足够多,您应该容易地看到一些琐碎的测试脚本有所不同.
So you should easily see a difference on some trivial test script as N
increases enough.
就个人而言,我通常喜欢干净"地使用C ++/STL类型的代码,以感觉像更多的C ++风格",但仍然会意识到性能下降.通常,这并不重要,因为C ++解决方案要比纯R解决方案中替换的解决方案要快.
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.