Code from this stack overflow thread
Last active
August 29, 2015 13:59
-
-
Save romainfrancois/10849651 to your computer and use it in GitHub Desktop.
rev with Rcpp and Rcpp11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
romain@naxos ~/git/dplyr $ RcppScript /tmp/rev.cpp | |
> library(microbenchmark) | |
> x = rep(c(1, 2, 3, 4), each = 1e+05) | |
> rev2 = function(x) { | |
+ nx = length(x) | |
+ y = vector("numeric", nx) | |
+ for (i in nx) y[i] = x[nx - i + 1] | |
+ y | |
+ } | |
> rev3 = function(x) { | |
+ x[length(x):1] | |
+ } | |
> rev4 = function(x) { | |
+ .subset(x, length(x):1) | |
+ } | |
> microbenchmark(rev(x), rev2(x), rev3(x), rev4(x), | |
+ Rcpprev2s(x), RcppRev3(x), CppRev(x)) | |
Unit: microseconds | |
expr min lq median uq max neval | |
rev(x) 2890.765 3166.3695 4067.1065 4367.688 31645.36 100 | |
rev2(x) 565.817 635.6205 978.3925 1974.910 29135.17 100 | |
rev3(x) 2874.174 3141.3745 4033.4480 4358.533 33939.75 100 | |
rev4(x) 2907.989 3209.4560 4143.6140 4343.261 31891.12 100 | |
Rcpprev2s(x) 540.032 667.2825 708.8470 1822.555 27664.31 100 | |
RcppRev3(x) 602.216 616.5715 632.3385 1242.201 30543.52 100 | |
CppRev(x) 1495.572 1722.6400 1835.0080 2833.515 30584.69 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
romain@naxos ~/git/dplyr $ Rcpp11Script /tmp/rev.cpp | |
> library(microbenchmark) | |
> x = rep(c(1, 2, 3, 4), each = 1e+05) | |
> rev2 = function(x) { | |
+ nx = length(x) | |
+ y = vector("numeric", nx) | |
+ for (i in nx) y[i] = x[nx - i + 1] | |
+ y | |
+ } | |
> rev3 = function(x) { | |
+ x[length(x):1] | |
+ } | |
> rev4 = function(x) { | |
+ .subset(x, length(x):1) | |
+ } | |
> microbenchmark(rev(x), rev2(x), rev3(x), rev4(x), | |
+ Rcpprev2s(x), RcppRev3(x), CppRev(x)) | |
Unit: microseconds | |
expr min lq median uq max neval | |
rev(x) 2963.545 3742.1755 4081.4955 4771.596 13583.02 100 | |
rev2(x) 493.937 675.3870 1444.4155 1798.793 10781.08 100 | |
rev3(x) 2901.671 3799.8440 4062.8965 4609.037 13660.12 100 | |
rev4(x) 2888.391 3177.7595 3951.7435 4371.901 21455.32 100 | |
Rcpprev2s(x) 454.979 550.4395 580.9995 1324.806 10015.25 100 | |
RcppRev3(x) 407.545 465.3735 489.7015 1006.441 10448.26 100 | |
CppRev(x) 1335.404 1621.1900 1889.8265 2336.011 11690.65 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Rcpp.h> | |
using namespace Rcpp; | |
// [[Rcpp::export]] | |
IntegerVector Rcpprev2s(IntegerVector x){ | |
int nx = x.size(); | |
IntegerVector y(nx); | |
for(int i = 0; i<nx; i++){ | |
y[i] = x[nx-i-1]; | |
} | |
return(y); | |
} | |
// [[Rcpp::export]] | |
IntegerVector RcppRev3(IntegerVector x) { | |
return rev(x); | |
} | |
// [[Rcpp::export]] | |
std::vector<int> CppRev(std::vector<int> & x) { | |
std::reverse(x.begin(), x.end()); | |
return x; | |
} | |
/*** R | |
library(microbenchmark) | |
x = rep(c(1L,2L,3L,4L), each=1e5) | |
rev2 = function(x){ | |
nx = length(x) | |
y = vector("numeric", nx) | |
for(i in nx) y[i] = x[nx-i+1] | |
y | |
} | |
rev3 = function(x){ | |
x[length(x):1] | |
} | |
rev4 = function(x){ | |
.subset(x, length(x):1) | |
} | |
microbenchmark(rev(x), rev2(x), rev3(x), rev4(x), | |
Rcpprev2s(x), RcppRev3(x), CppRev(x)) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment