Single vs double quotes and performance?

Hello all,

When defining literal strings I tend to use single quotes instead of
double quotes. The only time I use double quotes is when I need my
string to be scanned for variable substitution or special characters.

The reasoning behind this is that I read somewhere that the Ruby
interpreter handles single quotes faster than double quotes because
it doesn’t have to parse the string looking for special characters.
Still I come across lots of code which never uses single quotes and
am guessing that the speed gain is not even noticeable in most cases.

Is my nitpicking going to help me out in the long run or am I just
waisting my time here. This minor issue has been itching my brain
for a while now. :slight_smile:

Thanks,

Emiel

···


E F van de Laar
+31648183479
www.il.fontys.nl/~emiel

$ cat single_quotes
100_000.times { puts ‘Hello World’ }
$ cat double_quotes
100_000.times { puts “Hello World” }

$ time ruby single_quotes
[snip]
real 0m22.812s
user 0m2.220s
sys 0m1.660s
$ time ruby double_quotes
real 0m21.614s
user 0m2.100s
sys 0m1.600s

The significant measure is ‘user’. You can expect to save 0.1 seconds
after printing 100 thousand strings.

I hope this helps. :slight_smile:

···

On Thu, Jan 23, 2003 at 03:40:22AM +0900, E F van de Laar wrote:

Is my nitpicking going to help me out in the long run or am I just
waisting my time here. This minor issue has been itching my brain
for a while now. :slight_smile:


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

Hi,

···

At Thu, 23 Jan 2003 03:40:22 +0900, E F van de Laar wrote:

The reasoning behind this is that I read somewhere that the Ruby
interpreter handles single quotes faster than double quotes because
it doesn’t have to parse the string looking for special characters.
Still I come across lots of code which never uses single quotes and
am guessing that the speed gain is not even noticeable in most cases.

There are some special characters even in single quoted
literals. No significant difference unless you use millions of
\ and #.


Nobu Nakada

Hello E,

Wednesday, January 22, 2003, 9:40:22 PM, you wrote:

The reasoning behind this is that I read somewhere that the Ruby
interpreter handles single quotes faster than double quotes because
it doesn’t have to parse the string looking for special characters.
Still I come across lots of code which never uses single quotes and
am guessing that the speed gain is not even noticeable in most cases.

why not test this himself? :slight_smile:

···


Best regards,
Bulat mailto:bulatz@integ.ru

I’ve often wondered the same thing. The difference doesn’t appear to be much:

require 'benchmark’
include Benchmark

data = ((0…9).to_a.to_s) * 2000000
double = %!"#{data}"!
single = “’#{data}’”

bm do |x|
x.report(“double:”) { eval(double) }
x.report(“single:”) { eval(single) }
end

  user     system      total        real

double: 0.560000 0.050000 0.610000 ( 0.614154)
single: 0.510000 0.060000 0.570000 ( 0.577753)

20,000,000 characters have to be scanned to do the eval, and the difference
is miniscule. I ran this test ten times, and the single was always faster, but
it’s such a negligible difference that it doesn’t matter.

···

Travis Whitton whitton@atlantic.net

I hope you din’t mind me nitpicking. In each of those programs, you
only define a single string. You are not measuring the time to
construct a string with single-vs-double quotes, you are measuring the
time to print 100000 already-constructed strings!

Perhaps the 0.1 second time difference is explained by the fact that
the double-quoted version needs to look for more special characters.
But I doubt it.

Gavin

···

On Thursday, January 23, 2003, 6:53:11 AM, Daniel wrote:

On Thu, Jan 23, 2003 at 03:40:22AM +0900, E F van de Laar wrote:

Is my nitpicking going to help me out in the long run or am I just
waisting my time here. This minor issue has been itching my brain
for a while now. :slight_smile:

$ cat single_quotes
100_000.times { puts ‘Hello World’ }
$ cat double_quotes
100_000.times { puts “Hello World” }

$ time ruby single_quotes
[snip]
real 0m22.812s
user 0m2.220s
sys 0m1.660s
$ time ruby double_quotes
real 0m21.614s
user 0m2.100s
sys 0m1.600s

The significant measure is ‘user’. You can expect to save 0.1 seconds
after printing 100 thousand strings.

Hi,

···

At Thu, 23 Jan 2003 04:53:11 +0900, Daniel Carrera wrote:

On Thu, Jan 23, 2003 at 03:40:22AM +0900, E F van de Laar wrote:

Is my nitpicking going to help me out in the long run or am I just
waisting my time here. This minor issue has been itching my brain
for a while now. :slight_smile:

$ cat single_quotes
100_000.times { puts ‘Hello World’ }
$ cat double_quotes
100_000.times { puts “Hello World” }

Performance difference between single and double quotes does
exist only at compile time. Those two ASTs are absolutely
equivalent. Nothing differs while execution.

If you really want to measure the difference, you have to feed
very huge literals, or eval them.


Nobu Nakada

‘single’ doesn’t look like a single-quoted string to me.

Gavin

···

On Saturday, January 25, 2003, 6:29:28 AM, Travis wrote:

I’ve often wondered the same thing. The difference doesn’t appear to be much:

require ‘benchmark’
include Benchmark

data = ((0…9).to_a.to_s) * 2000000
double = %!“#{data}”!
single = “‘#{data}’”

Gavin Sinclair wrote:

require ‘benchmark’
include Benchmark

data = ((0…9).to_a.to_s) * 2000000
double = %!“#{data}”!
single = “‘#{data}’”

‘single’ doesn’t look like a single-quoted string to me.

it is when it’s eval’ed.

···


dave

::slap:: Sorry for the red herring.

Gavin

···

On Saturday, January 25, 2003, 12:14:10 PM, David wrote:

Gavin Sinclair wrote:

require ‘benchmark’
include Benchmark

data = ((0…9).to_a.to_s) * 2000000
double = %!“#{data}”!
single = “‘#{data}’”

‘single’ doesn’t look like a single-quoted string to me.

it is when it’s eval’ed.