Skip to content

Instantly share code, notes, and snippets.

@ginolhac
Created March 22, 2018 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ginolhac/3b65c31ca59524d9023f95eb060ddf0c to your computer and use it in GitHub Desktop.
Save ginolhac/3b65c31ca59524d9023f95eb060ddf0c to your computer and use it in GitHub Desktop.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
SEXP with_string(CharacterVector names) {
int size = names.size();
std::vector<String> tmp;
String strtmp;
CharacterVector out_vec(tmp.begin(), tmp.end());
for (int i = 0; i < size; ++i) {
strtmp = names[i];
tmp.push_back(strtmp);
}
return out_vec;
}
// [[Rcpp::export]]
SEXP with_stdstr(CharacterVector names) {
int size = names.size();
std::vector<std::string> tmp;
String strtmp;
CharacterVector out_vec(tmp.begin(), tmp.end());
for (int i = 0; i < size; ++i) {
strtmp = names[i];
tmp.push_back(strtmp);
}
return out_vec;
}
/*** R
system.time(a <- with_string(paste0("col_", 1:50000)))
system.time(b <- with_stdstr(paste0("col_", 1:50000)))
identical(a, b)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment