Skip to content

Instantly share code, notes, and snippets.

@olivierlacan
Created September 29, 2014 15:40
Show Gist options
  • Select an option

  • Save olivierlacan/4034cd788c26a0e88b57 to your computer and use it in GitHub Desktop.

Select an option

Save olivierlacan/4034cd788c26a0e88b57 to your computer and use it in GitHub Desktop.
Does it really make sense to optimize for a 1.01x speed improvement by using single-quoted strings instead of double-quoted Strings in Ruby (to avoid interpolation scanning in the double-quoted strings)? I think the answer is no.
$ ruby string_benchmark.rb
Calculating -------------------------------------
single quote 128800 i/100ms
double quote 128351 i/100ms
-------------------------------------------------
single quote 6144842.6 (±5.4%) i/s - 30654400 in 5.008373s
double quote 6089768.4 (±6.4%) i/s - 30290836 in 5.000430s
Comparison:
single quote: 6144842.6 i/s
double quote: 6089768.4 i/s - 1.01x slower
require 'benchmark/ips'
Benchmark.ips do |x|
x.report("single quote") { '' }
x.report("double quote") { "" }
x.compare!
end
@sferik
Copy link
Copy Markdown

sferik commented Sep 29, 2014

@olivierlacan 1% is well within the margin of error. I bet if you ran it a few more times, double quotes would seem faster. They’re the same.

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