6

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?

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
BAR
  • 15,909
  • 27
  • 97
  • 185

1 Answers1

13

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.

Adam Lindberg
  • 16,447
  • 6
  • 65
  • 85