Created
July 18, 2019 14:20
-
-
Save wilsonfreitas/19c0cf4ea6717d05bd110941aedeedd4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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