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?
An atom is a literal, a constant with name. An atom is to be enclosed in single quotes (') if it does not begin with a lower-case letter or if it contains other characters than alphanumeric characters, underscore (_), or @. The following program is an example of how atoms can be used in Erlang.
Types describe sets of Erlang terms. Types consist of, and are built from, a set of predefined types, for example, integer(), atom(), and pid(). Predefined types represent a typically infinite set of Erlang terms that belong to this type. For example, the type atom() denotes the set of all Erlang atoms.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With