Clojure 的代理与 Scala 的演员相比如何?

发布于 2024-09-10 01:54:47 字数 1083 浏览 3 评论 0原文

我在 Scala 中编写了环形网络拓扑 的模拟(源代码在这里) (Scala 2.8 RC7) 和 Clojure (来源此处)(Clojure 1.1),用于比较 Actor 和 Agent。

当我将网络中的节点数量从 100 增加到 1000000 时,Scala 版本显示的消息交换率几乎恒定,而 Clojure 版本显示的消息交换率随着节点数量的增加而降低。此外,在单次运行期间,Clojure 版本中的消息速率会随着时间的推移而降低。

所以我很好奇 Scala 的 Actor 与 Clojure 的 Agent 相比如何?代理本身的并发性是否比参与者低,或者代码编写效率低下(自动装箱?)?

PS:我注意到 Scala 版本中的内存使用量随着节点数量的增加而增加很多(100 万个节点> 500 MB),而 Clojure 版本使用的内存要少得多(100 万个节点约 100 MB) 。

编辑:

两个版本都在同一 JVM 上运行,所有 JVM 参数以及 Actor 和 Agent 配置参数均设置为默认值。在我的机器上,Scala 版本对于 100 到 100 万个节点始终提供约 5000 条消息/秒的消息速率,而 Clojure 版本从 100 个节点的 60000 条消息/秒开始,然后降至 100 万个节点的 200 条消息/秒。

编辑 2

事实证明我的 Clojure 版本编写效率低下。我将 nodes 集合的类型从 list 更改为 vector,现在它显示出一致的行为:100 个节点为 100000 条消息/秒,80000 条消息/ 100000 个节点的秒数。所以 Clojure Agent 似乎比 Scala Actors 更快。我也更新了链接的来源。

I wrote a simulation of the Ring network topology in Scala (source here) (Scala 2.8 RC7) and Clojure (source here) (Clojure 1.1) for a comparison of Actors and Agents.

While the Scala version shows almost constant message exchange rate as I increase the number of nodes in network from 100 to 1000000, the Clojure version shows message rates which decrease with the increase in the number of nodes. Also during a single run, the message rate in Clojure version decreases as the time passes.

So I am curious about how the Scala's Actors compare to Clojure's Agents? Are Agents inherently less concurrent than Actors or is the code inefficiently written (autoboxing?)?

PS: I noted that the memory usage in the Scala version increases a lot with the increase in the number of nodes (> 500 MB for 1 million nodes) while the Clojure one uses much less memory (~ 100 MB for 1 million nodes).

Edit:

Both the versions are running on same JVM with all the JVM args and Actor and Agent configuration parameters set as default. On my machine, the Scala version gives a message rate of around 5000 message/sec consistently for 100 to 1 million nodes, whereas the Clojure version starts with 60000 message/sec for 100 nodes which decreases to 200 messages/sec for 1 million nodes.

Edit 2

Turns out that my Clojure version was inefficiently written. I changed the type of nodes collection from list to vector and now it shows consistent behaviour: 100000 message/sec for 100 nodes and 80000 message/sec for 100000 nodes. So Clojure Agents seem to be faster than Scala Actors. I have updated the linked sources too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羅雙樹 2024-09-17 01:54:47

[免责声明:我是 Akka 团队的成员]

Clojure Agent 与 Scala actor 是不同的,尤其是如果您考虑谁控制行为的话。在 Agent 中,行为在外部定义并推送到 Agent,而在 Actor 中,行为在 Actor 内部定义。

在不了解您的代码的情况下,我真的不能说太多,您是否使用相同的 JVM 参数、相同的预热、Actors 的合理设置与 Agents 的合理设置,或者它们是单独调整的?

作为旁注:
Akka 在这里有一个环形工作台的实现:http://github.com/jboner/akka-bench/tree/master/ring/

看看结果会很有趣与您机器上的 Clojure 测试进行比较。

[Disclaimer: I'm on the Akka team]

A Clojure Agent is a different beast from a Scala actor, most notably if you think about who controls the behavior. In Agents the behavior is defined outside and is pushed to the Agent, and in Actors the behavior is defined inside the Actor.

Without knowing anything about your code I really cannot say much, are you using the same JVM parameters, warming things up the same, sensible settings for Actors vs. sensible settings for Agents, or are they tuned separately?

As a side note:
Akka has an implementation of the ring bench located here: http://github.com/jboner/akka-bench/tree/master/ring/

Would be interesting to see what the result is compared to your Clojure test on your machine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文