Skip to content

Instantly share code, notes, and snippets.

@wilsonfreitas
Created July 18, 2019 14:20
Show Gist options
  • Save wilsonfreitas/19c0cf4ea6717d05bd110941aedeedd4 to your computer and use it in GitHub Desktop.
Save wilsonfreitas/19c0cf4ea6717d05bd110941aedeedd4 to your computer and use it in GitHub Desktop.
library(compiler)
library(foreach)
f.original <- function() {
x <- 0
for (p in 1:2) {
for (i in 1:500) {
for (j in 1:5000) {
x <- x + i * j
}
}
}
x
}
f.foreach <- function() {
x <- 0
foreach(p = 1:2, .combine = rbind) %do%
for (i in 1:500) {
for (j in 1:5000) {
x <- x + i * j
}
}
x
}
f.cmpfun <- function(x) {
f <- cmpfun(function(x) {
for (i in 1:500) {
for (j in 1:5000) {
x <- x + i * j
}
}
x
})
f(f(0))
}
library(microbenchmark)
microbenchmark(f.original(),f.foreach(),f.cmpfun(), times=100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment