Parallel map operations?

好久不见. 提交于 2019-12-03 08:45:57

问题


Does Scala provide a way to execute parallel map operations as part of the standard language?

For example, given:

scala> val a = List((1,2), (3,4), (3,6))
a: List[(Int, Int)] = List((1,2), (3,4), (3,6))

I can do:

scala> a.map(tup => tup._1 + tup._2)
res0: List[Int] = List(3, 7, 9)

However, to the best of my knowledge this maps the provided function over the list objects sequentially. Is there a built-in way to have the function applied to each element in a separate thread (or equivalent), and the results then gathered into a resulting list?


回答1:


If you add par then you will get a parallel collection and operations on it will be processed in parallel. To convert back to a normal collection call a toList.

So your code would look like:

a.par.map(tup => tup._1 + tup._2).toList

Or a .seq to get a Sequential Collection (the opposite of a Parallel Collection).

a.par.map(tup => tup._1 + tup._2).seq

Also, check the documentation.




回答2:


par splits your list for processing over several threads. You can then regulate how the threading is done by modyfying the tasksupport member of the resultant ParSeq.



来源:https://stackoverflow.com/questions/20092855/parallel-map-operations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!