为什么使用并行集合不能更快?
我只是想测试一下并行集合,我使用了以下代码行(在 REPL 中):
(1 to 100000).par.filter(BigInt(_).isProbablePrime(100))
against:
(1 to 100000).filter(BigInt(_).isProbablePrime(100))
但是并行版本并不更快。事实上,它甚至感觉有点慢(但我还没有真正测量过)。
有人对此有解释吗?
编辑1:是的,我确实有一个多核处理器
编辑2:好的,我自己“解决”了这个问题。 isProbablePrime 的实现似乎是问题所在,而不是并行集合。我用另一个函数替换了 isProbablePrime 来测试素数,现在我得到了预期的加速。
I just wanted to test the parallel collections a bit and I used the following line of code (in REPL):
(1 to 100000).par.filter(BigInt(_).isProbablePrime(100))
against:
(1 to 100000).filter(BigInt(_).isProbablePrime(100))
But the parallel version is not faster. In fact it even feels a bit slower (But I haven't really measured that).
Has anyone an explanation for that?
Edit 1: Yes, I do have a multi-core processor
Edit 2: OK, I "solved" the problem myself. The implementation of isProbablePrime
seems to be the problem and not the parallel collections. I replaced isProbablePrime
with another function to test for primality and now I get an expected speedup.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论是顺序范围还是并行范围,
filter
都会生成一个向量数据结构 - 分别是Vector
或ParVector
。这是从范围集合生成的并行向量的一个已知问题 - 并行向量的转换器方法(例如
filter
)不会并行构造向量。已经开发出一种允许高效并行构建向量的解决方案,但尚未实施。我建议您提交票据,以便可以在下一个版本中修复它。
Both with sequential and parallel ranges,
filter
will generate a vector data structure - aVector
or aParVector
, respectively.This is a known problem with parallel vectors that get generated from range collections - transformer methods (such as
filter
) for parallel vectors do not construct the vector in parallel.A solution for this that allows efficient parallel construction of vectors has already been developed, but was not yet implemented. I suggest you file a ticket, so that it can be fixed for the next release.