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

1

5

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?

Pulsatory answered 20/11, 2014 at 4:29 Comment(2)
well, gosh, why don't you just try it out?Tundra
@CarlWitthoft, I did some benchmark tests in simple situations, but there's no notable difference at all. That's why I want to know about it in more general sense.Pulsatory
T
10

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.

Tier answered 20/11, 2014 at 4:34 Comment(1)
This assumes that the vector was passed to the C++ program. What about a program that only uses such vectors internally or one which does pass them but is so time consuming that the one-time passing is negligible relative to the rest of the computation? Which would be faster then?Quinze

© 2022 - 2024 — McMap. All rights reserved.