Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library('microbenchmark')
- library('Rcpp')
- library('RcppParallel')
- library('foreach')
- library('doParallel')
- library('iterators')
- sourceCpp('testrcpp.cpp')
- ##~~~~~~~~~~~~~~~~~~~R parallel~~~~~~~~~~~~~~~~~
- SqrtRPara <- function(orig, n = 8) {
- registerDoParallel(cores = n)
- itx <- iter(orig)
- res <- foreach(i = itx, .combine = c) %dopar% {
- return(sqrt(i))
- }
- ## stop multiple cores
- stopImplicitCluster()
- return(res)
- }
- SqrtR <- function(orig) {
- res <- numeric(length(orig))
- for (i in orig) {
- res[i] <- sqrt(orig[i])
- }
- return(res)
- }
- ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ##~~~~~~~~~~~~~~~~test~~~~~~~~~~~~~~~~~~~~~
- tmp1 <- runif(10e3)
- all.equal(SqrtCpp(tmp1),
- sqrt(tmp1),
- SqrtCppPara(tmp1))
- microbenchmark(
- SqrtCpp(tmp1),
- sqrt(tmp1),
- SqrtCppPara(tmp1)
- )
- all.equal(SqrtCpp(tmp1),
- sqrt(tmp1),
- SqrtR(tmp1),
- SqrtRPara(tmp1),
- SqrtCppPara(tmp1))
- microbenchmark(
- SqrtCpp(tmp1),
- sqrt(tmp1),
- SqrtRPara(tmp1),
- SqrtR(tmp1),
- SqrtCppPara(tmp1)
- )
- ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Add Comment
Please, Sign In to add comment