Writing Efficient R Code
Writing Efficient R Code
R version
# Print the R version details using version
version
//3
Elapsed time
# Load the package
library(microbenchmark)
# Print compare
compare
//3
DataCamp hardware
# Load the package
library(benchmarkme)
//2
//4
//2
Row timings
# Which is faster, mat[1, ] or df[1, ]?
microbenchmark(mat[1, ], df[1, ])
//2
//3
Profvis in action
# Load the profvis package
library(profvis)
# Print no_of_cores
no_of_cores
//2
//2
//3
Moving to parApply
# Determine the number of available cores.
detectCores()
Using parSapply()
library("parallel")
# Create a cluster via makeCluster (2 cores)
cl <- makeCluster(2)
Timings parSapply()
# Set the number of games to play
no_of_games <- 1e5
## Set up cluster
cl <- makeCluster(4)
clusterExport(cl, "play")
## Stop cluster
stopCluster(cl)