Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang: atoms or integers

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?

like image 265
BAR Avatar asked Jan 24 '12 04:01

BAR


People also ask

Is Erlang an atom?

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.

Does Erlang have different types?

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.


1 Answers

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.

like image 198
Adam Lindberg Avatar answered Oct 01 '22 16:10

Adam Lindberg