Skip to content

Instantly share code, notes, and snippets.

@DavisVaughan
Created February 18, 2018 17:14
Show Gist options
  • Save DavisVaughan/ac3b2dfc6bd071191f1cf2a5d700e258 to your computer and use it in GitHub Desktop.
Save DavisVaughan/ac3b2dfc6bd071191f1cf2a5d700e258 to your computer and use it in GitHub Desktop.
library(Rcpp)
library(microbenchmark)
cppFunction(
'int looped_sum(int x, const int num_loops) {
for(int i = 0; i < num_loops; i++) {
x = x + 1;
}
return x;
}')
looped_sum_R <- function(x, num_loops) {
for(i in 1L:num_loops) {
x <- x + 1L
}
x
}
microbenchmark(
looped_sum(0L, 50000000L),
looped_sum_R(0L, 50000000L),
times = 10
)
#> Unit: microseconds
#> expr min lq mean
#> looped_sum(0L, 50000000L) 1.188 1.536 111.1451
#> looped_sum_R(0L, 50000000L) 1138032.985 1152609.126 1259103.4133
#> median uq max neval
#> 13.735 23.507 988.772 10
#> 1176141.634 1363335.973 1627560.344 10
#' Created on 2018-02-18 by the [reprex package](http://reprex.tidyverse.org) (v0.1.1.9000).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment