Erlang:原子或整数
我想传递一条消息,
{up, Distance}
{down, Distance}
我也可以这样做,
{1, Distance}
{-1, Distance}
关键区别是一个是原子,另一个是整数。阅读此处的手册页:
http://www.erlang.org/doc/efficiency_guide/advanced .html
整数和原子都在内存中占用 1 个字。然而他们提到了原子表并需要引用它。
我的问题是,每次使用原子时都会引用原子表吗?我的哪个例子最有效?
I want to pass around a message as such
{up, Distance}
{down, Distance}
I could also do this as such
{1, Distance}
{-1, Distance}
The key difference is one is an atom and other an integer. Reading the man pages here:
http://www.erlang.org/doc/efficiency_guide/advanced.html
both an integer and atom take up 1 word in memory. However they mention an atom table and needing to reference it.
My question is, does the atom table get referenced each and every time an atom is used? Which of my examples is the most efficient?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他们将同样高效。当与其他术语进行模式匹配时,使用原子的整数表示。原子表仅在打印原子或通过网络发送原子时使用(这些例外情况是使用原子会稍微慢一些)。
在这种情况下,优先考虑可读性而不是性能。
They will be equally efficient. The integer representation of an atom is used when pattern matching against other terms. The atom table is only used when printing atoms or sending them over the network (these are exceptions where it will be marginally slower to use atoms).
Favor readability over performance in this case.