I want to create a C++
function that raises each element in x
to power
and takes the mean. I've created three versions:
- power_mean_R:
R
solution --mean(x^power)
- power_mean_C:
C++
solution - power_mean_C_2arg:
C++
solution with extrapower
argument
The extra power
argument seems to drastically slow down the function to the point that it's slower than the R implementation. Is this a reality of using Rcpp
or is there something I can improve in my code?
#library(Rcpp)
cppFunction(
'double power_mean_C_2arg(NumericVector x, double power) {
int n = x.size();
double total = 0;
for(int i=0; i<n; ++i) {
total += pow(x[i], power);
}
return total / n;
}'
)
cppFunction(
'double power_mean_C(NumericVector x) {
int n = x.size();
double total = 0;
for(int i=0; i<n; ++i) {
total += pow(x[i], 2);
}
return total / n;
}'
)
power_mean_R <- function(x, power) {
mean(x^power)
}
bench::mark(
R = power_mean_R(1:100, p = 2),
C = power_mean_C(1:100),
C2arg = power_mean_C_2arg(x = 1:100, p = 2)
)
expression min median `itr/sec` mem_alloc `gc/sec` n_itr n_gc total_time result memory
<bch:expr> <bch:> <bch:> <dbl> <bch:byt> <dbl> <int> <dbl> <bch:tm> <list> <list>
1 R 5.91µs 6.91µs 112386. 1.27KB 0 10000 0 89ms <dbl … <Rpro…
2 C 2.87µs 3.54µs 231281. 3.32KB 23.1 9999 1 43.2ms <dbl … <Rpro…
3 C2arg 6.02µs 6.89µs 116187. 3.32KB 0 10000 0 86.1ms <dbl … <Rpro…