Symbol or Strings: Which Provides Better Program Performance?
First of all, what is a SYMBOL?
A Symbol object is created by prefixing an operator, string, variable, constant, method, class, and a module name with a colon. Symbols are names and an internal identification. A few key characteristics of symbols:
· Symbols are unique. Each symbol is stored in the SYMBOL TABLE. So, if there is a method called control_movie , there is automatically a symbol :control_movie. Ruby’s interpreted, so it keeps its Symbol Table handy at all times. You can find out what’s on it at any given moment by calling Symbol.all_symbols.
· Symbols are constant. They do not contain values or objects, like variables do. Instead, they’re used as a consistent name within a code. They refer to the same object throughout the Ruby program.
· Symbols are immutable. Unlike strings, you are unable to modify symbols. The following example illustrates this:
Why use SYMBOLS?
What is the main advantage of using symbols over strings? Symbols use less memory and hence help in saving the runtime speed of your application. So, how are symbols stored in memory?
First, a few Terms to Know:
Ruby Heap: The memory Ruby manages and to which we have access is organized on the Ruby heap. Each heap contains slots. Each Ruby slot references one object and references the location in the computer’s heap (where the data of the string or symbol is stored).
The RValue is the value that is to the right-hand side of an assignment operator. For example; x = “horse”. In this example, “horse” is the RValue (x is the LValue.)
When a string is created, the following occurs:
1. A free slot on the Ruby heap becomes filled.
2. Memory to store the string’s data is allocated and put on the System Heap (separate from the Ruby heap. The system heap is in your computer.) The Ruby slot references the location of where the data lives in the computer memory. Once the string is no longer needed it is garbage collected (the system used by Ruby to free up memory space through the removal of unused data).
3. Once variables are cleaned up, space on a cell is freed up.
Whereas, when a symbol is created, Step 1 occurs and the string and integer representation of the symbol is saved in a single Ruby slot. The symbol is stored in a Symbol Table. Garbage collection does not occur on symbols.
Because there is an integer and string representation of symbols, symbols generally have performance benefits. Each symbol is identified to the programmer by its name (for instance, :mysymbol), but the program can identify it by its numeric representation, which of course is a quicker lookup.
When two strings are compared, pointers must walk both strings, looking for a mismatch. When two Ruby symbols are compared, if their numeric representation is equal, then the symbols are equal. If you were to use :mysymbol twenty times in your program, every usage of :mysymbol would refer to exactly the same object with exactly the same numeric representation and exactly the same string representation. This can enhance performance.
Therefore, when do we use a string versus a symbol?
· If the contents of the object are important, use a string.
· If the identity of the object is important, use a symbol.
The following are a few useful websites to learn more about Ruby Symbols:
13 Ways of Looking at a Ruby Symbol
The Ruby_Newbie Guide to Symbols