Skip to content

Instantly share code, notes, and snippets.

@bloudermilk
Created September 9, 2012 00:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bloudermilk/3681645 to your computer and use it in GitHub Desktop.
Save bloudermilk/3681645 to your computer and use it in GitHub Desktop.
Ruby single quotes vs. double quotes
require "benchmark"
Benchmark.bm(7) do |bench|
bench.report("single") do
1_000_000.times do
'This is a string of substantial length. I doubt you will have many string
literals in your code that are longer than this, but if there are actually
costs to parsing double quoted strings this should exacerbate that.'
end
end
bench.report("double") do
1_000_000.times do
"This is a string of substantial length. I doubt you will have many string
literals in your code that are longer than this, but if there are actually
costs to parsing double quoted strings this should exacerbate that."
end
end
end

Here are the results of the above benchmark running against three different Ruby VMs.

ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin11.4.0]

             user     system      total        real
single   0.170000   0.000000   0.170000 (  0.171696)
double   0.170000   0.000000   0.170000 (  0.171394)

ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]

              user     system      total        real
single    0.190000   0.000000   0.190000 (  0.189292)
double    0.180000   0.000000   0.180000 (  0.184996)

jruby 1.7.0.preview1 (ruby-1.9.3-p203) (2012-05-19 00c8c98) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_35) [darwin-x86_64-java]

              user     system      total        real
single    0.420000   0.010000   0.430000 (  0.221000)
double    0.240000   0.010000   0.250000 (  0.118000)

As you can see, double quotes are actually faster in all three VMs. They are twice as fast in JRuby but only marginally faster in the two CRuby VMs. I don't have Rubinius on hand or I'd test that as well.

So, there you have it. Double quotes interpolate strings easily, support several nifty escape sequences like \n and \t, and for whatever reason they're faster than their counterpart. Maybe because there's 2X the quotes!

@patbenatar
Copy link

ruby 2.0.0p247

              user     system      total        real
single    0.110000   0.000000   0.110000 (  0.102108)
double    0.100000   0.000000   0.100000 (  0.102095)

@Baozi2
Copy link

Baozi2 commented Jan 6, 2018

ruby 2.4.1
if double quotes has interpolation

Benchmark.bm(7) do |bench|
  bench.report("single") do
    1_000_000.times do
      'This is a string of substantial length. I doubt you will have many string
      literals in your code that are longer than this, but if there are actually
      costs to parsing double quoted strings this should exacerbate that.'
    end
  end

  bench.report("double") do
    1_000_000.times do |i|
      "|i| This is a string of substantial length. I doubt you will have many string
      literals in your code that are longer than this, but if there are actually
      costs to parsing double quoted strings this should exacerbate that."
    end
  end
end
          user     system      total        real
          user     system      total        real

single 0.070000 0.000000 0.070000 ( 0.071082)
double 0.080000 0.000000 0.080000 ( 0.079633)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment