rrcpprcpp11

Is there performance difference between NumericVector and 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?


Solution

  • 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.