1. Learn
  2. /
  3. Courses
  4. /
  5. Parallel Programming in R

Connected

Exercise

Microbenchmark revenues

You work as a Data Analyst for an online seller. You have queried sales data for different products sold during a month. This is available in your workspace as a list, ls_sales. Each element of this list is a vector of revenues for a given product.

You would like to see how the revenue grew day by day. This mean calculating a cumulative sum. Base R has a function called cumsum() to do the job. But you would like to see if parallelization can help. You want to apply cumsum() to every element of ls_sales sequentially and in parallel and compare the results. parallel and microbenchmark packages have been loaded for you.

Instructions

100 XP
  • Pass the sequential and parallel versions as arguments to a microbenchmark() call.
  • Generate a cluster of all available cores minus two.
  • Use the cluster to apply cumsum() to ls_sales in parallel using parLapply().
  • Stop the cluster once computation is done.