Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Created April 4, 2018 01:08
Show Gist options
  • Save hrbrmstr/276072b0a83ee25328aaa1c046880ce8 to your computer and use it in GitHub Desktop.
Save hrbrmstr/276072b0a83ee25328aaa1c046880ce8 to your computer and use it in GitHub Desktop.
Rcpp::cppFunction('int rcpp_find2(NumericVector needle, NumericVector haystack) {
NumericVector::iterator it;
it = std::search(haystack.begin(), haystack.end(), needle.begin(), needle.end());
int pos = it - haystack.begin() + 1;
if (pos > haystack.size()) pos = -1;
return(pos);
}')
set.seed(010)
haystack <- sample(0:12, size = 2000, replace = TRUE)
needle <- c(2L, 0L, 9L)
microbenchmark::microbenchmark(rcpp_find2(needle, haystack))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment