"" vs ''

I know this is a very simply question, but I've seen many different responses.

Isn't it better to use 'some string' instead of "some string". Because the "" goes through a lot more working interpreting any variable that might be in the string, etc. Where as '' is just literal.

Thanks for your help.

Thank You,
Ben Johnson
E: bjohnson@contuitive.com

Aesthetics aside, Ruby optomizes double quoted strings that don't have any
interpolation so the performance difference between the two is irrelevant.

marcel

···

On Fri, Jul 21, 2006 at 03:48:09AM +0900, Ben Johnson wrote:

I know this is a very simply question, but I've seen many different
responses.

Isn't it better to use 'some string' instead of "some string".
Because the "" goes through a lot more working interpreting any
variable that might be in the string, etc. Where as '' is just literal.

--
Marcel Molina Jr. <marcel@vernix.org>

Ben Johnson wrote:

I know this is a very simply question, but I've seen many different responses.

Isn't it better to use 'some string' instead of "some string". Because the "" goes through a lot more working interpreting any variable that might be in the string, etc. Where as '' is just literal.

Thanks for your help.

Thank You,
Ben Johnson
E: bjohnson@contuitive.com

I like to think it makes a difference, but...

Don't know if this is a good benchmark or not (probably not):

require 'benchmark'
include Benchmark

bmbm(10) { |r|
        r.report('Literal') {
                1000000.times {
                        'asdjasdjpaidjpoasjdsadsadajpsodjaposdjpaojvpdoasjpdoajs
pdoBaspdojaspodjpaowjeAo'
                }
        }
        r.report('Interpolated') {
                1000000.times {
                        "asdjasdjpaidjpoasjdsadsadajpsodjaposdjpaojvpdoasjpdoajs
pdoBaspdojaspodjpaowjeAo"
                }
        }
}

Rehearsal ------------------------------------------------
Literal 0.480000 0.000000 0.480000 ( 0.494254)
Interpolated 0.480000 0.000000 0.480000 ( 0.492916)
--------------------------------------- total: 0.960000sec

                   user system total real
Literal 0.490000 0.000000 0.490000 ( 0.546283)
Interpolated 0.490000 0.000000 0.490000 ( 0.501239)

'...' goes through processing too, just less. \' and \\ are special inside '...'.

Is there a time difference though? Let's ask Ruby:

#!/usr/bin/env ruby -w

require "benchmark"

TESTS = 10_000_000.freeze
Benchmark.bmbm(10) do |results|
   results.report("single:") { TESTS.times { 'My String' } }
   results.report("double:") { TESTS.times { "My String" } }
end
# >> Rehearsal ---------------------------------------------
# >> single: 2.840000 0.010000 2.850000 ( 2.861201)
# >> double: 2.870000 0.000000 2.870000 ( 2.885730)
# >> ------------------------------------ total: 5.720000sec
# >>
# >> user system total real
# >> single: 2.870000 0.010000 2.880000 ( 2.891390)
# >> double: 2.850000 0.000000 2.850000 ( 2.869507)

__END__

Doesn't look like it. Guess Ruby is pretty smart about handling this.

End facts. Begin opinions...

I use to use '...' all the time thinking it was a good programming habit. Unfortunately, it just meant I had to switch to "..." every time I belatedly realized I would need some interpolation. So, as I've gotten lazier, I've pretty much switched to using "..." all the time.

James Edward Gray II

···

On Jul 20, 2006, at 1:48 PM, Ben Johnson wrote:

I know this is a very simply question, but I've seen many different responses.

Isn't it better to use 'some string' instead of "some string". Because the "" goes through a lot more working interpreting any variable that might be in the string, etc. Where as '' is just literal.

I usually use double quotes since my strings often have apostrophes in 'em.

'Tis better'n always escapin' 'em anyways. Maybe it's just me tho'.

---John

···

On 7/20/06, Ben Johnson <bjohnson@contuitive.com> wrote:

I know this is a very simply question, but I've seen many different
responses.

Isn't it better to use 'some string' instead of "some string".
Because the "" goes through a lot more working interpreting any
variable that might be in the string, etc. Where as '' is just literal.

I know this is a very simply question, but I've seen many different
responses.

Isn't it better to use 'some string' instead of "some string".
Because the "" goes through a lot more working interpreting any
variable that might be in the string, etc. Where as '' is just literal.

There is no difference at all after parsing, both get turned into a NODE_STR (unless there are interpolations):

%{"abc"}.parse_to_nodes.transform

=> [:str, {:lit=>"abc"}]

%{'abc'}.parse_to_nodes.transform

=> [:str, {:lit=>"abc"}]

And I don't think that there is any significant performance difference while parsing, so use whatever you like more.

Dominik

···

On Thu, 20 Jul 2006 20:48:09 +0200, Ben Johnson <bjohnson@contuitive.com> wrote:

Use " by default:

    - It makes the code more aestethic.
    - It helps when you have to put some inside like #{} '.

···

--
Upper reality >oftware.
Dave - Skp Core.

--
Email.it, the professional e-mail, gratis per te: http://www.email.it/f

Sponsor:
Le speciali Offerte di Benvenuto di Cassine di Pietra:
* scopra il gusto ed i vantaggi delle tradizioni contadine
*
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=3924&d=25-9

Use '' by default:

  - It's less work for the interpreter.

  - It alerts the reader to impending "#{magic}".

-r

···

--
http://www.cfcl.com/rdm Rich Morin
http://www.cfcl.com/rdm/resume rdm@cfcl.com
http://www.cfcl.com/rdm/weblog +1 650-873-7841

Technical editing and writing, programming, and web development

I often end up typing ' simply because it's an unmodified key on my
keyboard, unlike " which necessitates pressing shift as well.

Here's a little trick for when you have a '-quoted string that you
want to add interpolation to: just add %:

before:
'foo #{3}' # => "foo #{3}"

after:
%'foo #{3}' # => "foo 3"

I'm not saying you should do it all the time, but it's occasionally
nice to save a few keystrokes when playing around with some code.

Paul.

···

On 25/09/06, dave <dave.m@email.it> wrote:

  Use " by default:

    - It makes the code more aestethic.
    - It helps when you have to put some inside like #{} '.

What makes you say this?

#!/usr/bin/env ruby -w

require "benchmark"

TESTS = 1_000_000
Benchmark.bmbm(10) do |results|
   results.report("double:") { TESTS.times { "James" } }
   results.report("single:") { TESTS.times { 'James' } }
end
# >> Rehearsal ---------------------------------------------
# >> double: 0.270000 0.000000 0.270000 ( 0.267826)
# >> single: 0.260000 0.000000 0.260000 ( 0.266784)
# >> ------------------------------------ total: 0.530000sec
# >>
# >> user system total real
# >> double: 0.270000 0.000000 0.270000 ( 0.268957)
# >> single: 0.290000 0.000000 0.290000 ( 0.286691)

James Edward Gray II

···

On Sep 25, 2006, at 12:03 PM, Rich Morin wrote:

Use '' by default:

  - It's less work for the interpreter.

If the interpreter sees a single-quoted string, it knows
that it doesn't need to scan the string for things (such
as escape sequences) to expand. I agree that this effect
will be lost in the noise, in most cases, but why make the
interpreter do extra work?

The real point, in any case, is one of making things clear
to the reader. If a quoted string uses double quotes, the
reader has to look it over to determine whether any magic
is being performed. This wastes some of the reader's time
(and no, I don't have a benchmark for this. :-).

-r

···

At 3:10 AM +0900 9/26/06, James Edward Gray II wrote:

On Sep 25, 2006, at 12:03 PM, Rich Morin wrote:

Use '' by default:

  - It's less work for the interpreter.

What makes you say this?

--
http://www.cfcl.com/rdm Rich Morin
http://www.cfcl.com/rdm/resume rdm@cfcl.com
http://www.cfcl.com/rdm/weblog +1 650-873-7841

Technical editing and writing, programming, and web development

James Edward Gray II <james@grayproductions.net> writes:

Use '' by default:

  - It's less work for the interpreter.

What makes you say this?

#!/usr/bin/env ruby -w

require "benchmark"

TESTS = 1_000_000
Benchmark.bmbm(10) do |results|
  results.report("double:") { TESTS.times { "James" } }
  results.report("single:") { TESTS.times { 'James' } }
end

You really should compare the parse-trees of these before even trying
to benchmark it that way...

···

On Sep 25, 2006, at 12:03 PM, Rich Morin wrote:

James Edward Gray II

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Hi,

At Tue, 26 Sep 2006 04:09:54 +0900,
Rich Morin wrote in [ruby-talk:216346]:

···

At 3:10 AM +0900 9/26/06, James Edward Gray II wrote:
>> Use '' by default:
>>
>> - It's less work for the interpreter.
>
> What makes you say this?

If the interpreter sees a single-quoted string, it knows
that it doesn't need to scan the string for things (such
as escape sequences) to expand. I agree that this effect
will be lost in the noise, in most cases, but why make the
interpreter do extra work?

It knows that it does need to scan the string always for the
terminator, and even escaped quotes.

--
Nobu Nakada

Rich Morin wrote:
...

The real point, in any case, is one of making things clear
to the reader. If a quoted string uses double quotes, the
reader has to look it over to determine whether any magic
is being performed. This wastes some of the reader's time
(and no, I don't have a benchmark for this. :-).

True, but syntax hiliting can help spot the #{...}.

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Du hast ist recht!

Any differences due to parsing are going to happen when the source
code is parsed, which will happen before the benchmark is run.

I believe that this gives a better handle on the relative parsing
overhead, there doesn't seem to be much difference.

rick@frodo:/public/rubyscripts$ cat slbench.rb
require 'benchmark'

TESTS = 1_000_000
Benchmark.bmbm(10) do |results|
  results.report("double:") {TESTS.times{eval("\"James\"")}}
  results.report("single:") {TESTS.times{eval('\'James\'')}}
end
rick@frodo:/public/rubyscripts$ ruby slbench.rb
Rehearsal ---------------------------------------------
double: 31.990000 1.030000 33.020000 ( 55.318747)
single: 32.080000 1.120000 33.200000 ( 55.743170)
----------------------------------- total: 66.220000sec

                user system total real
double: 32.160000 1.040000 33.200000 ( 56.097062)
single: 32.150000 1.090000 33.240000 ( 53.597692)
rick@frodo:/public/rubyscripts$

···

On 9/25/06, Christian Neukirchen <chneukirchen@gmail.com> wrote:

James Edward Gray II <james@grayproductions.net> writes:

> On Sep 25, 2006, at 12:03 PM, Rich Morin wrote:
>
>> Use '' by default:
>>
>> - It's less work for the interpreter.
>
> What makes you say this?
>
> #!/usr/bin/env ruby -w
>
> require "benchmark"
>
> TESTS = 1_000_000
> Benchmark.bmbm(10) do |results|
> results.report("double:") { TESTS.times { "James" } }
> results.report("single:") { TESTS.times { 'James' } }
> end

You really should compare the parse-trees of these before even trying
to benchmark it that way...

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

IPMS/USA Region 12 Coordinator
http://ipmsr12.denhaven2.com/

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/

James Edward Gray II <james@grayproductions.net> writes:

>
>> Use '' by default:
>>
>> - It's less work for the interpreter.
>
> What makes you say this?
>
> #!/usr/bin/env ruby -w
>
> require "benchmark"
>
> TESTS = 1_000_000
> Benchmark.bmbm(10) do |results|
> results.report("double:") { TESTS.times { "James" } }
> results.report("single:") { TESTS.times { 'James' } }
> end

You really should compare the parse-trees of these before even trying
to benchmark it that way...

In the non-interpolation case the parse-trees are identical:

% cat strings.rb
f('James', "James")

% cat strings.rb | parse_tree_show -f
[[:fcall, :f, [:array, [:str, "James"], [:str, "James"]]]]

···

On Tue, Sep 26, 2006 at 05:00:17AM +0900, Christian Neukirchen wrote:

> On Sep 25, 2006, at 12:03 PM, Rich Morin wrote:
> James Edward Gray II
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Incorrect. For identical string contents the interpreter does the same amount of work. The parser may do less work, but you only parse once.

$ parse_tree_show -f
["foo", 'foo', "#{foo}", '#{foo}']
[[:array,
   [:str, "foo"],
   [:dstr, "", [:vcall, :foo]],
   [:str, "\#{foo}"]]]

···

On Sep 25, 2006, at 12:09 PM, Rich Morin wrote:

At 3:10 AM +0900 9/26/06, James Edward Gray II wrote:

On Sep 25, 2006, at 12:03 PM, Rich Morin wrote:

Use '' by default:

  - It's less work for the interpreter.

What makes you say this?

If the interpreter sees a single-quoted string, it knows
that it doesn't need to scan the string for things (such
as escape sequences) to expand. I agree that this effect
will be lost in the noise, in most cases, but why make the
interpreter do extra work?

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

"Rick DeNatale" <rick.denatale@gmail.com> writes:

You really should compare the parse-trees of these before even trying
to benchmark it that way...

Du hast ist recht!

Drop the "ist".

Any differences due to parsing are going to happen when the source
code is parsed, which will happen before the benchmark is run.

I believe that this gives a better handle on the relative parsing
overhead, there doesn't seem to be much difference.

rick@frodo:/public/rubyscripts$ cat slbench.rb
require 'benchmark'

TESTS = 1_000_000
Benchmark.bmbm(10) do |results|
results.report("double:") {TESTS.times{eval("\"James\"")}}
results.report("single:") {TESTS.times{eval('\'James\'')}}
end
rick@frodo:/public/rubyscripts$ ruby slbench.rb
Rehearsal ---------------------------------------------
double: 31.990000 1.030000 33.020000 ( 55.318747)
single: 32.080000 1.120000 33.200000 ( 55.743170)
----------------------------------- total: 66.220000sec

               user system total real
double: 32.160000 1.040000 33.200000 ( 56.097062)
single: 32.150000 1.090000 33.240000 ( 53.597692)
rick@frodo:/public/rubyscripts$

And now the difference gets lost in noise because the strings aren't
long enough. :slight_smile:

In the end, it doesn't matter.

···

Rick DeNatale

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

I was speaking of the entire ruby executable as the
interpreter. Clearly, the parsing subsystem is the
only part (if any) that does more work. However, as
I pointed out, this isn't the real issue.

Indicating the possibility of "magic" behavior, without
the need for the reader to look inside the quotes, is
my principle concern.

-r

···

At 5:29 PM +0900 9/29/06, Eric Hodel wrote:

Incorrect. For identical string contents the interpreter
does the same amount of work. The parser may do less work,
but you only parse once.

--
http://www.cfcl.com/rdm Rich Morin
http://www.cfcl.com/rdm/resume rdm@cfcl.com
http://www.cfcl.com/rdm/weblog +1 650-873-7841

Technical editing and writing, programming, and web development