Ruby : Difference between single/double quotation

Not so with double quoted strings. Double quoted strings are pre-processed in some familiar ways:

    • \n becomes a single linefeed (also known as a newline) character,
      • So while the string ‘abc\n’ is five characters long (the last two characters being a backslash and the letter “n”), the string “abc\n” is only four characters long (the last character being a linefeed character).
      • puts 'a\nb' # just print a\nb
        puts "a\nb" # print a, then b at newline
         
        puts "\1" -> get special character
        puts '\1' -> get \1
    • while \t becomes a tab.
    • Finally, you may have noticed that no \n appeared at the end of the ‘hello world’ string that we fed to puts. Nevertheless, puts printed our message with a newline. It turns out that puts is actually fairly clever and will stick a newline character on the end of any output that lacks one. This behavior is not necessarily desirable for precision formatting. (Don’t get Confused)
2.1.1 :001 > puts 'hello world!'
hello world!
 => nil
2.1.1 :002 > puts "hello world!"
hello world!
 => nil
2.1.1 :003 > puts "hello world!"; puts "this is Shiva"
hello world!
this is Shiva
 => nil
2.1.1 :004 > puts 'hello world!'; puts 'this is Shiva, again'
hello world!
this is Shiva, again
 => nil
2.1.1 :005 >

Conclusion:

I too thought that single quoted strings might be quicker to parse for Ruby. It doesn’t seem to be the case.

Anyway, I think the above benchmark are measuring the wrong thing, though. It stands to reason that either versions will be parsed into the same internal string representations so to get the answer as to which is quicker to parse, we shouldn’t be measuring performance with string variables, but rather Ruby’s speed of parsing strings.

generate.rb:
10000.times do
  ('a'..'z').to_a.each {|v| print "#{v}='This is a test string.'\n" }
end
 
#Generate sample ruby code with lots of strings to parse
$ ruby generate.rb > single_q.rb
#Get the double quote version
$ tr \' \" < single_q.rb > double_q.rb
 
#Compare execution times
$ time ruby single_q.rb
 
real    0m0.978s
user    0m0.920s
sys     0m0.048s
$ time ruby double_q.rb
 
real    0m0.994s
user    0m0.940s
sys     0m0.044s

Repeated runs don’t seem to make much difference. It still takes pretty much the same time to parse either version of the string.

References :

Book – Design pattern in Ruby

Stack Question: http://stackoverflow.com/questions/6395288/double-vs-single-quotes

http://stackoverflow.com/questions/1836467/is-there-a-performance-gain-in-using-single-quotes-vs-double-quotes-in-ruby

Leave a Reply

Fill in your details below or click an icon to log in:

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