Get startedGet started for free

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

View Course

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 to 1e5.
  • Use system.time() to time play() being repeatedly called in serial.
    • Call sapply() with 1:no_of_games and the play() wrapper function.
    • Assign the result to serial.
    • Wrap this call in system.time() to time it.
  • Create a 4 core cluster object and export the play() function to it.
  • Use system.time() to time play() being repeatedly called in parallel.
    • Rework the code you used to create serial, but make it work in parallel.

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)
Edit and Run Code