-
Notifications
You must be signed in to change notification settings - Fork 3
/
comparison.R
88 lines (73 loc) · 2.09 KB
/
comparison.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
library(RcppFastFloat)
library(microbenchmark)
library(Rcpp)
N <- 1e6
set.seed(42) # does not matter but ensure sample() shuffle fixed
invec <- sample(sqrt(seq(1:N)))
input <- as.character(invec)
sourceCpp(code="
#include <Rcpp.h>
#include <fast_float/fast_float.h>
// [[Rcpp::depends(RcppFastFloat)]]
// [[Rcpp::export]]
void fastfloat(Rcpp::CharacterVector invec, Rcpp::NumericVector outvec) {
size_t n = invec.size();
for (size_t i=0; i<n; i++) {
double d;
std::string s(invec[i]);
fast_float::from_chars(s.data(), s.data()+s.size(), d);
outvec[i] = d;
}
}
// [[Rcpp::export]]
void strtod(Rcpp::CharacterVector invec, Rcpp::NumericVector outvec) {
size_t n = invec.size();
for (size_t i=0; i<n; i++) {
std::string s(invec[i]);
outvec[i] = std::strtod(s.c_str(), NULL);
}
}
// [[Rcpp::export]]
void atof(Rcpp::CharacterVector invec, Rcpp::NumericVector outvec) {
size_t n = invec.size();
for (size_t i=0; i<n; i++) {
std::string s(invec[i]);
outvec[i] = std::atof(s.c_str());
}
}
// [[Rcpp::export]]
void sscanf(Rcpp::CharacterVector invec, Rcpp::NumericVector outvec) {
size_t n = invec.size();
for (size_t i=0; i<n; i++) {
std::string s(invec[i]);
double d;
std::sscanf(s.c_str(), \"%lf\", &d);
outvec[i] = d;
}
}
// [[Rcpp::export]]
void stod(Rcpp::CharacterVector invec, Rcpp::NumericVector outvec) {
size_t n = invec.size();
for (size_t i=0; i<n; i++) {
std::string s(invec[i]);
outvec[i] = std::stod(s);
}
}
")
outff <- double(N)
outst <- double(N)
outaf <- double(N)
outsf <- double(N)
outsd <- double(N)
res <- microbenchmark(scanf = sscanf(input, outsf),
atof = atof(input, outaf),
strtod = strtod(input, outst),
stod = stod(input, outsd),
fastfloat = fastfloat(input, outff))
## ensure correct parsing for all
stopifnot(`ff`=all.equal(invec,outff),
`st`=all.equal(invec,outst),
`af`=all.equal(invec,outaf),
`sf`=all.equal(invec,outsf),
`sd`=all.equal(invec,outsf))
print(res)