Skip to content

Instantly share code, notes, and snippets.

@yht
Last active May 9, 2017 05:38
Show Gist options
  • Save yht/8a3d1acc9c31559a72849093ab3cb9e3 to your computer and use it in GitHub Desktop.
Save yht/8a3d1acc9c31559a72849093ab3cb9e3 to your computer and use it in GitHub Desktop.
Loop Time Test
R version 3.3.3 (2017-03-06) -- "Another Canoe"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> system.time(rm(list=ls()))
user system elapsed
0 0 0
> system.time(a <- seq(1, 10000, 0.5))
user system elapsed
0 0 0
> system.time(
+ for(i in 1:length(a)) {
+ b <- ifelse((a %/% 1)==1, T, F)
+ }
+ )
user system elapsed
22.12 0.00 22.14
> library(foreach)
foreach: simple, scalable parallel programming from Revolution Analytics
Use Revolution R for scalability, fault tolerance and more.
http://www.revolutionanalytics.com
> system.time(
+ foreach(a) %do%
+ (
+ c <- ifelse((a %/% 1)==1, T, F)
+ )
+ )
user system elapsed
20.01 0.15 20.17
> system.time(
+ foreach(a) %dopar%
+ (
+ d <- ifelse((a %/% 1)==1, T, F)
+ )
+ )
user system elapsed
19.44 0.00 19.43
Warning message:
executing %dopar% sequentially: no parallel backend registered
> library("doParallel")
Loading required package: iterators
Loading required package: parallel
> cl <- makePSOCKcluster(12)
> registerDoParallel(cl)
> system.time(
+ foreach(a) %dopar%
+ (
+ e <- ifelse((a %/% 1)==1, T, F)
+ )
+ )
user system elapsed
7.75 3.43 11.49
> stopCluster(cl)
> rm(cl)
> library(doSNOW)
Loading required package: snow
Attaching package: ‘snow’
The following objects are masked from ‘package:parallel’:
clusterApply, clusterApplyLB, clusterCall, clusterEvalQ, clusterExport, clusterMap, clusterSplit, makeCluster, parApply, parCapply, parLapply, parRapply, parSapply, splitIndices, stopCluster
> cl <- makeCluster(12, type="SOCK")
> system.time(
+ foreach(a) %dopar%
+ (
+ f <- ifelse((a %/% 1)==1, T, F)
+ )
+ )
user system elapsed
7.66 3.62 11.64
> stopCluster(cl)
> rm(cl)
>
system.time(rm(list=ls()))
system.time(a <- seq(1, 10000, 0.5))
system.time(
for(i in 1:length(a)) {
b <- ifelse((a %/% 1)==1, T, F)
}
)
library(foreach)
system.time(
foreach(a) %do%
(
c <- ifelse((a %/% 1)==1, T, F)
)
)
system.time(
foreach(a) %dopar%
(
d <- ifelse((a %/% 1)==1, T, F)
)
)
library("doParallel")
cl <- makePSOCKcluster(12)
registerDoParallel(cl)
system.time(
foreach(a) %dopar%
(
e <- ifelse((a %/% 1)==1, T, F)
)
)
stopCluster(cl)
rm(cl)
library(doSNOW)
cl <- makeCluster(12, type="SOCK")
system.time(
foreach(a) %dopar%
(
f <- ifelse((a %/% 1)==1, T, F)
)
)
stopCluster(cl)
rm(cl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment