Por que meu código Rcpp não é muito mais rápido?
Para praticar meu C ++, estou tentando converter algum código R em Rcpp. O código é um algoritmo ganancioso implementado emesta resposta.
Em seguida, consulte meu código Rcpp (em um arquivo .cpp) e algumas referências dos dois códigos:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
List create_groups2(const NumericVector& x, double thr) {
int n = x.size();
List res(n);
int c = 0;
double sum;
std::list<double> x2(n);
std::copy(x.begin(), x.end(), x2.begin()); // copy x in x2
x2.sort(std::greater<double>()); // sort in descending order
std::list<double>::iterator it;
NumericVector x3(n);
int i = 0, c2;
while (x2.size()) {
sum = 0; c2 = 0;
for (it = x2.begin(); it != x2.end();) {
if ((sum + *it) <= thr) {
sum += *it;
x3[i] = *it;
i++; c2++;
it = x2.erase(it);
if (sum >= thr) break;
} else {
it++;
}
}
res[c] = x3[seq(i - c2, i - 1)];
c++;
}
return res[seq_len(c) - 1];
}
/*** R
y <- c(18, 15, 11, 9, 8, 7)
create_groups2(sample(y), 34)
create_groups <- function(input, threshold) {
input <- sort(input, decreasing = TRUE)
result <- vector("list", length(input))
sums <- rep(0, length(input))
for (k in input) {
i <- match(TRUE, sums + k <= threshold)
if (!is.na(i)) {
result[[i]] <- c(result[[i]], k)
sums[i] <- sums[i] + k
}
}
result[sapply(result, is.null)] <- NULL
result
}
x_big <- round(runif(1e4, min = 1, max = 34))
all.equal(
create_groups(x_big, 34),
create_groups2(x_big, 34)
)
microbenchmark::microbenchmark(
R = create_groups(x_big, 34),
RCPP = create_groups2(x_big, 34),
times = 20
)
*/
Para esse tipo de problema (repetindo e repetindo um vetor), eu esperava que minha versão do Rcpp fosse muito mais rápida, mas recebo esse resultado para o benchmark:
Unit: milliseconds
expr min lq mean median uq max neval cld
R 584.0614 590.6234 668.4479 717.1539 721.9939 729.4324 20 b
RCPP 166.0554 168.1817 170.1019 170.3351 171.8251 174.9481 20 a
Alguma idéia de por que meu código Rcpp não é muito mais rápido que a versão R?