Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

rcpp

rcpp11

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?

like image 847
Kun Ren Avatar asked Dec 25 '22 02:12

Kun Ren


1 Answers

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.

like image 180
Dirk Eddelbuettel Avatar answered Jan 13 '23 17:01

Dirk Eddelbuettel