Last active
September 13, 2020 07:43
-
-
Save artemklevtsov/64751bbfb561a18aac6fc76c094f2078 to your computer and use it in GitHub Desktop.
charToRaw and charToRaw Rcpp implementation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [[Rcpp::plugins(cpp17)]] | |
#include <Rcpp.h> | |
// [[Rcpp::export(rng = false)]] | |
Rcpp::RawVector Cpp_charToRaw1(const std::string& s) { | |
Rcpp::RawVector res(s.begin(), s.end()); | |
return res; | |
} | |
// [[Rcpp::export(rng = false)]] | |
Rcpp::RawVector Cpp_charToRaw2(const char* s) { | |
Rcpp::RawVector res(s, s + std::strlen(s)); | |
return res; | |
} | |
// [[Rcpp::export(rng = false)]] | |
std::vector<unsigned char> Cpp_charToRaw3(const char* s) { | |
std::vector<unsigned char> res(s, s + std::strlen(s)); | |
return res; | |
} | |
// [[Rcpp::export(rng = false)]] | |
std::vector<unsigned char> Cpp_charToRaw4(const char* s) { | |
std::string_view sv(s); | |
std::vector<unsigned char> res(sv.begin(), sv.end()); | |
return res; | |
} | |
// [[Rcpp::export(rng = false)]] | |
Rcpp::RawVector Cpp_charToRaw5(const char* s) { | |
std::string_view sv(s); | |
Rcpp::RawVector res(sv.begin(), sv.end()); | |
return res; | |
} | |
// [[Rcpp::export(rng = false)]] | |
std::string Cpp_rawToChar1(Rcpp::RawVector x) { | |
std::string out(reinterpret_cast<const char*>(x.begin()), x.size()); | |
return out; | |
} | |
// [[Rcpp::export(rng = false)]] | |
std::string Cpp_rawToChar2(Rcpp::RawVector x) { | |
std::string out((x.begin()), x.end()); | |
return out; | |
} | |
// [[Rcpp::export(rng=false)]] | |
std::string Cpp_rawToChar3(const std::vector<unsigned char>& x) { | |
return std::string(x.begin(), x.end()); | |
} | |
// [[Rcpp::export(rng=false)]] | |
const char* Cpp_rawToChar4(const std::vector<unsigned char>& x) { | |
const char* res = reinterpret_cast<const char*>(x.data()); | |
return res; | |
} | |
/***R | |
s = "Test string. Test string. Test string. Test string." | |
bench::mark( | |
Cpp_charToRaw1(s), | |
Cpp_charToRaw2(s), | |
Cpp_charToRaw3(s), | |
Cpp_charToRaw4(s), | |
Cpp_charToRaw5(s), | |
charToRaw(s), | |
iterations = 100000 | |
) | |
r = charToRaw(s) | |
bench::mark( | |
Cpp_rawToChar1(r), | |
Cpp_rawToChar2(r), | |
Cpp_rawToChar3(r), | |
Cpp_rawToChar4(r), | |
rawToChar(r), | |
iterations = 100000 | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.