Let's say I have a somewhat large (several millions of items, or so) list of strings. Is it a good idea to run something like this:
val updatedList = myList.par.map(someAction).toList
Or would it be a better idea to group the list before running ...par.map(
, like this:
val numberOfCores = Runtime.getRuntime.availableProcessors
val updatedList =
myList.grouped(numberOfCores).toList.par.map(_.map(someAction)).toList.flatten
UPDATE:
Given that someAction
is quite expensive (comparing to grouped
, toList
, etc.)
Run par.map
directly, as it already takes the number of cores into account. However, do not keep a List
, as that requires a full copy to make into a parallel collection. Instead, use Vector
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With