Timings parSapply()
Running the dice game is embarrassingly parallel. These types of simulations
usually (but not always) produce a good speed-up. As before, we can use microbenchmark()
or system.time()
. For simplicity, we'll use system.time()
in this exercise.
This exercise is part of the course
Writing Efficient R Code
Exercise instructions
The play()
function has been defined in your workspace. We'll assume that we want to play one hundred thousand games.
- Set
no_of_games
to1e5
. - Use
system.time()
to timeplay()
being repeatedly called in serial.- Call
sapply()
with1:no_of_games
and theplay()
wrapper function. - Assign the result to
serial
. - Wrap this call in
system.time()
to time it.
- Call
- Create a 4 core cluster object and export the
play()
function to it. - Use
system.time()
to timeplay()
being repeatedly called in parallel.- Rework the code you used to create
serial
, but make it work in parallel.
- Rework the code you used to create
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Set the number of games to play
no_of_games <- ___
# Time serial version
system.time(serial <- sapply(1:___, function(i) play()))
# Set up cluster
cl <- makeCluster(___)
clusterExport(cl, "play")
# Time parallel version
system.time(par <- ___)
# Stop cluster
stopCluster(cl)