CodeProject · Ruby

Ruby Symbols Performance Analysis

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.


2 thoughts on “Ruby Symbols Performance Analysis

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s