cppFunction('NumericVector pdistC(double x, NumericVector ys) {
int n = ys.size();
NumericVector out(n);
for(int i = 0; i < n; ++i) {
out[i] = sqrt(pow(ys[i] - x, 2.0));
}
return out;
}')
y <- runif(1e6)
bench::mark(
pdistR(0.5, y),
pdistC(0.5, y)
)[1:6]
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 pdistR(0.5, y) 4.24ms 5.26ms 192. 7.63MB 54.3
#> 2 pdistC(0.5, y) 4.06ms 4.11ms 236. 7.63MB 40.6