Is There a Performance Gain in Using Single Quotes VS Double Quotes in Ruby

Is there a performance gain in using single quotes vs double quotes in ruby?

$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.0.0]

$ cat benchmark_quotes.rb
# As of Ruby 1.9 Benchmark must be required
require 'benchmark'

n = 1000000
Benchmark.bm(15) do |x|
x.report("assign single") { n.times do; c = 'a string'; end}
x.report("assign double") { n.times do; c = "a string"; end}
x.report("concat single") { n.times do; 'a string ' + 'b string'; end}
x.report("concat double") { n.times do; "a string " + "b string"; end}
end

$ ruby benchmark_quotes.rb

user system total real
assign single 0.110000 0.000000 0.110000 ( 0.116867)
assign double 0.120000 0.000000 0.120000 ( 0.116761)
concat single 0.280000 0.000000 0.280000 ( 0.276964)
concat double 0.270000 0.000000 0.270000 ( 0.278146)

Note: I've updated this to make it work with newer Ruby versions, and cleaned up the header, and run the benchmark on a faster system.

This answer omits some key points. See especially these other answers concerning interpolation and the reason there is no significant difference in performance when using single vs. double quotes.

Why does Rubymine suggests I use single-quotes over double-quotes on strings?

Single quotes are preferred if there is no interpolation in the string. Ruby will work less (in theory) to output single quote strings which in turn will speed up your code some (again in theory). That is one reason why RubyMine suggests it.

Another reason is for plain readability. Which you can read about here in the style guide: Ruby Coding Style Guide

Benchmark tests has proven that speed gains from using single over double quoted strings is negligible compared to the actual execution time.

Ultimately the answer comes down to style. To learn about performance check out this question: Is there a performance gain in using single quotes vs double quotes in ruby?

When to use single vs. double quotes in a Rails app

The difference between using double quotes versus single quotes is that double quotes interpret escaped characters and single quotes preserve them.

Here is an example

 puts "i \n love \n ruby"
#i
#love
#ruby

and

 puts 'i \n love \n ruby'
#i \n love \n ruby

Is there any advantage to _ever_ using a single quote around a string in Ruby/Rails?

I usually follow the following rule:

never use double quotes (or %Q or %W) if you don't interpolate

The reason for this is that if you're trying to track down an error or a security bug, you immediately know when looking at the beginning of the string that there cannot possibly any code inside it, therefore the bug cannot be in there.

However, I also follow the following exception to the rule:

use double quotes if they make the code more readable

I.e. I prefer

"It's time"

over

'It\'s time'
%q{It's time}

It is technically true that single quoted strings are infinitesimally faster to parse than double quoted strings, but that's irrelevant because

  • the program only gets parsed once, during startup, there is no difference in runtime performance
  • the performance difference really is extremely small
  • the time taken to parse strings is irrelevant compared to the time taken to parse some of Ruby's crazier syntax

So, the answer to your question is: Yes, there is an advantage, namely that you can spot right away whether or not a string may contain code.

Why everyone use double quotes instead of simple quote?

Double quotes allows you to do interpolation : "Number of users : #{@count_user}"

Plus taking a look at the benchmarks, I'd say that at best it doesn't matter, the overhead is very small, and some benchmarks are actually faster with double quotes ...

Double vs single quotes

" " allows you to do string interpolation, e.g.:

world_type = 'Mars'
"Hello #{world_type}"

ruby coding convention for single or double quoting your strings

I don't think there is a strong convention within the entire community. From what I have seen, there seems to be a bias towards ignoring single quotes altogether and always using double quotes. In some circles, that is even a convention, but a localized one, not one for the whole community.

Personally, whenever I have several different ways to express the same thing, I tend to use those different ways to convey different semantics. (For example, I use curly braces vs. do/end in blocks to distinguish between blocks that are used for their side effects and blocks that are used for their return value.)

So, I only use double quotes when I actually want to use string interpolation or escaping, otherwise I use single quotes. That way, when I see a string I can immediately tell that there's no funny business going on, and there's no code hidden inside of it.

I am pragmatic, though. I very much prefer "It's a string!" over 'It\'s a string!' or %q[It's a string!].

Ruby's string interpolation seems to behave differently with single quotes vs. double quotes

Yes, the answer is that you cannot put variables in single quoted strings, eg string interpolation is not allowed in single quoted strings. Further, you might be interested, escape sequences do not work in single quoted strings (Except for escaping single quotes themselves like 'don\'t').

There is also some debate about whether or not there's performance benefits of single vs double quotes, but I'm not seeing any convincing cases.

Backslashes in single quoted strings vs. double quoted strings

I'd refer you to "Ruby Programming/Strings" for a very concise yet comprehensive overview of the differences.

From the reference:

puts "Betty's pie shop"

puts 'Betty\'s pie shop'

Because "Betty's" contains an apostrophe, which is the same character as the single quote, in the second line we need to use a backslash to escape the apostrophe so that Ruby understands that the apostrophe is in the string literal instead of marking the end of the string literal. The backslash followed by the single quote is called an escape sequence.

When should I use double or single quotes in JavaScript?

The most likely reason for use of single vs. double in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string.

Using the other type of quote as a literal:

alert('Say "Hello"');
alert("Say 'Hello'");

This can get complicated:

alert("It's \"game\" time.");
alert('It\'s "game" time.');

Another option, new in ECMAScript 6, is template literals which use the backtick character:

alert(`Use "double" and 'single' quotes in the same string`);
alert(`Escape the \` back-tick character and the \${ dollar-brace sequence in a string`);

Template literals offer a clean syntax for: variable interpolation, multi-line strings, and more.

Note that JSON is formally specified to use double quotes, which may be worth considering depending on system requirements.



Related Topics



Leave a reply



Submit