Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

Ruby Symbols Performance Analysis

, 11 Oct 2011
Rate this:
Please Sign up or sign in to vote.
Dear Reader, Today i was again skimming through the symbols in ruby and its advantage and then i began to see under which scenarios these folks claim its much better than the traditional way. So i started to write some small code chunks to test the performance on my laptop having i7 with 4 gigs [...

Dear Reader,

Today i was again skimming through the symbols in ruby and its advantage and then i began to see under which scenarios these folks claim its much better than the traditional way. So i started to write some small code chunks to test the performance on my laptop having i7 with 4 gigs RAM.

Reason behind i always do/think about these kinda stuffs is that i believe one must not only know about a feature but also should know when it is effective and when it is not. That way you can be a good developer and write good code. Like in C#, many people just say StringBuilder is pretty faster than Strings. Well not always true and Jon Skeet debunks it in detail.

So by doing some experiments with the code, I must tell you not always the symbol usage was much effective or atleast say performance boost. But that doesn’t mean it was poor compared to traditional way. So below is the code i am using to test the performance.

As you can see from the above image, i have displayed code as well as the output. In the code under if condition i am just comparing variable condition to “yes” string and to :yes symbol in another if condition. So after executing the above code, i got the results as displayed at the bottom of the image. Just observe at the last column under braces. Symbols is taking more time than the string literals. Please note that the iterationcount is just 1000. Upon increasing it to 100000 value, i get below results:

“ruby 1.9.2p290 (2011-07-09 revision 32553) [i486-linux]“

user     system      total        real
string literal  0.020000   0.000000   0.020000 (  0.023764)
via symbols…  0.020000   0.000000   0.020000 (  0.022699)

Now you can see a flipped result actually. String literal result is taking more time than symbols. Now lets add a body to the if condition in the above code and test the performance again, look at the code below:

As you can see from the code above, have set the iterationcount to 1000 and have added a small code to the if condition body. At first you might think the performance results vary by a slight margin. But actually as per the results i got, its pretty much big difference. Upon decreasing the count of the iteration, the margin difference between the samples goes very less. So it does not make any sense to choose which ever way.

So think about choosing which of these is perfect to your problem based on the frequency of the literal you have in your code. As per the above code, upon increasing the iterations the efficiency of the symbols is way better than string literal.

Thanks :)

P.S: Your valuable comments/votes are appreciated.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

zenwalker1985
Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.
 
Loves Trekking/Hiking, animals and nature.
 
A FOSS/Linux maniac by default Wink | ;)
 
An MVP aspirant and loves blogging -> https://adventurouszen.wordpress.com/
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 11 Oct 2011
Article Copyright 2011 by zenwalker1985
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid