并行映射操作? [英] Parallel map operations?

查看:154
本文介绍了并行映射操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala是否提供了一种方法来执行作为标准语言一部分的并行地图操作?

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

例如,给定:

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

我可以:

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?

推荐答案

如果你添加 par ,那么你将得到一个并行集合,并且对它的操作将被并行处理。要转换回正常的集合,请调用 toList

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

此外,

这篇关于并行映射操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆