Skip to content

Instantly share code, notes, and snippets.

@artemklevtsov
Last active September 13, 2020 07:43
Show Gist options
  • Save artemklevtsov/64751bbfb561a18aac6fc76c094f2078 to your computer and use it in GitHub Desktop.
Save artemklevtsov/64751bbfb561a18aac6fc76c094f2078 to your computer and use it in GitHub Desktop.
charToRaw and charToRaw Rcpp implementation.
// [[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
)
*/
@artemklevtsov
Copy link
Author

artemklevtsov commented Sep 30, 2019

> 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),
+  .... [TRUNCATED] 
# A tibble: 6 x 13
  expression          min median `itr/sec` mem_alloc `gc/sec`  n_itr  n_gc total_time result
  <bch:expr>        <bch> <bch:>     <dbl> <bch:byt>    <dbl>  <int> <dbl>   <bch:tm> <list>
1 Cpp_charToRaw1(s) 633ns  703ns  1081646.        0B      0   100000     0     92.5ms < [512 Cpp_charToRaw2(s) 525ns  596ns  1475564.        0B      0   100000     0     67.8ms < [513 Cpp_charToRaw3(s) 579ns  645ns  1314849.        0B      0   100000     0     76.1ms < [514 Cpp_charToRaw4(s) 569ns  637ns  1457694.        0B      0   100000     0     68.6ms < [515 Cpp_charToRaw5(s) 522ns  584ns  1582075.        0B     15.8  99999     1     63.2ms < [516 charToRaw(s)      382ns  442ns  2075378.        0B      0   100000     0     48.2ms < [51# … with 3 more variables: memory <list>, time <list>, gc <list>

> r = charToRaw(s)

> bench::mark(
+   Cpp_rawToChar1(r),
+   Cpp_rawToChar2(r),
+   Cpp_rawToChar3(r),
+   Cpp_rawToChar4(r),
+   rawToChar(r),
+   iterations = 100000
+ .... [TRUNCATED] 
# A tibble: 5 x 13
  expression          min median `itr/sec` mem_alloc `gc/sec`  n_itr  n_gc total_time result
  <bch:expr>        <bch> <bch:>     <dbl> <bch:byt>    <dbl>  <int> <dbl>   <bch:tm> <list>
1 Cpp_rawToChar1(r) 735ns  804ns  1114922.        0B      0   100000     0     89.7ms <chr2 Cpp_rawToChar2(r) 742ns  799ns  1136085.        0B     11.4  99999     1       88ms <chr3 Cpp_rawToChar3(r) 778ns  832ns  1127151.        0B      0   100000     0     88.7ms <chr4 Cpp_rawToChar4(r) 744ns  802ns  1139719.        0B     11.4  99999     1     87.7ms <chr5 rawToChar(r)      713ns  767ns  1188818.        0B      0   100000     0     84.1ms <chr# … with 3 more variables: memory <list>, time <list>, gc <list>
> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment