CodeProject · Ruby

Ruby’s Interpolation and its performance

Hi Reader,

In this article i shall be discussing on interpolation feature in ruby and performance measure on it. Basically i wanted to do this and googled in searching for any existing evidence by other folks. I didn’t find much blogs/articles on that. But only could find 1 thread on stackoverflow regarding the performance measurement.

Any ways in case if you didn’t know about interpolation, do sure check here, not only this feature is good in performance wise but also the code to read does look better and easy. Hence most ruby developers use interpolation to concatenate variables in strings.

Basically, if you just wanted to concatenate 1 variable to a string once in a while then you can go for + operator or << as well. No performance loss nor gain. But if you have more number of parameters and frequently if your going to append, then interpolation is the best way.

Since the SO thread is measured on slightly old version of ruby, so i wanted to do the same testing on my machine with latest stable version. So here is the code:

  1. p `ruby -v`.chomp()
  2. print \n
  3. require ‘benchmark’
  4. n = 10000000
  5. value = “1000000”
  6. Benchmark.bm do |x|
  7.   x.report(“assign single”) { n.times do; c = ‘string ‘ + value; end}
  8.   x.report(“single interp”) {n.times do; c= ‘string #{value}’; end}
  9.   x.report(“assign double”) {n.times do; c = “string” + value; end}
  10.   x.report(“double interp”){n.times do; c= “string #{value}”; end}
  11. end

Actually in the above code, in line 1 i am actually printing or executing a command via ruby code, then from line 3 onwards i am using the standard gem package provided in ruby called benchmark, hence i am importing it into my code using require  keyword just like using keyword in c# language. Benchmark.bm is a method in Benchmark module, which allows us to do some performance measurement over the code. This method provides in detailed information on time consumption. You can get detailed information about this module here.

Let me do some performance analysis as per the code written above. Upon executing, i get the below results:

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

user     system      total        real
assign single  3.840000   0.000000   3.840000 (  3.844673)
single interp  1.540000   0.000000   1.540000 (  1.545718)
assign double  3.960000   0.000000   3.960000 (  3.965057)
double interp  4.150000   0.010000   4.160000 (  4.167783)

So from the results output above it shows that single quoted strings takes less time compared to double quoted string (quoted) via + operator. In case of interpolation the same happens and infact there is a big differences between the the single interop and double interp line output values.

So my dear friend, the bottom line is if your using concatenation every where then go for interpolation. I am sure the future versions of ruby as well do try to improve the performance more. Its pretty sad to say that this beautiful feature is not provided in c# by compiler team. So i had to write an extension method to do the same work in c#.

Thanks 🙂

P.S: Your valuable comments are much appreciated.

Advertisements

2 thoughts on “Ruby’s Interpolation and its performance

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s