Skip to content

Instantly share code, notes, and snippets.

@dfalbel
Created March 20, 2017 15:04
Show Gist options
  • Save dfalbel/5caf65d2ed24bcd04c661bab04588a6a to your computer and use it in GitHub Desktop.
Save dfalbel/5caf65d2ed24bcd04c661bab04588a6a to your computer and use it in GitHub Desktop.
parallel_sort
Rcpp::sourceCpp('t.cpp')
x <- runif(1000000)
m <- microbenchmark::microbenchmark(
psort(x),
csort(x),
sort(x)
)
x <- 1000000:1
m <- microbenchmark::microbenchmark(
psort(x),
csort(x),
sort(x)
)
// [[Rcpp::depends(RcppParallel)]]
#include <Rcpp.h>
#include <RcppParallel.h>
using namespace Rcpp;
// This is a simple example of exporting a C++ function to R. You can
// source this function into an R session using the Rcpp::sourceCpp
// function (or via the Source button on the editor toolbar). Learn
// more about Rcpp at:
//
// http://www.rcpp.org/
// http://adv-r.had.co.nz/Rcpp.html
// http://gallery.rcpp.org/
//
// [[Rcpp::export]]
NumericVector psort(NumericVector x) {
NumericVector y = clone(x);
tbb::parallel_sort(y.begin(), y.end());
return y;
}
// [[Rcpp::export]]
NumericVector csort(NumericVector x) {
NumericVector y = clone(x);
std::sort(y.begin(), y.end());
return y;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment