Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Use of single quotes versus double quotes

by lokiloki (Beadle)
on Dec 08, 2006 at 18:56 UTC ( #588670=perlquestion: print w/ replies, xml ) Need Help??
lokiloki has asked for the wisdom of the Perl Monks concerning the following question:

One of the O'Reilly Perl books says that for efficiencies sake I should always use single quotes, when possible, over double quotes. Are there huge time savings in use of print 'hello world'; over print "hello world";

Comment on Use of single quotes versus double quotes
Select or Download Code
Re: Use of single quotes versus double quotes
by liverpole (Monsignor) on Dec 08, 2006 at 19:00 UTC
    Hi lokiloki,

    Not really -- it's more a matter of taste.  I'm sure you're aware that double quotes allow interpolation, whereas single quotes don't, right?  Some people prefer to use single quotes when they know for sure they're not doing any interpolation.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Use of single quotes versus double quotes
by grinder (Bishop) on Dec 08, 2006 at 19:06 UTC
    Are there huge time savings

    In this case, absolutely none. Both forms are compiled down into constant strings:

    • Double quoted:
      % perl -MO=Concise -e 'print qq{hello, world}' 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <@> print vK ->6 3 <0> pushmark s ->4 4 <$> const(PV "hello, world") s ->5
    • versus single quoted:
      % perl -MO=Concise -e 'print q{hello, world}' 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 5 <@> print vK ->6 3 <0> pushmark s ->4 4 <$> const(PV "hello, world") s ->5 -e syntax OK

    The compiler will also fold things like "hello, world\n" and 'hello, world' . "\n" into the same constant strings. It's only when interpolation in a double string comes into play that things change:

    % perl -MO=Concise -e 'print qq{hello, $world\n}' a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 9 <@> print vK ->a 3 <0> pushmark s ->4 - <1> ex-stringify sK/1 ->9 - <0> ex-pushmark s ->4 8 <2> concat[t2] sKS/2 ->9 6 <2> concat[t1] sK/2 ->7 4 <$> const(PV "hello, ") s ->5 - <1> ex-rv2sv sK/1 ->6 5 <$> gvsv(*world) s ->6 7 <$> const(PV "\n") s ->8 -e syntax OK

    ... which is only slightly different to...

    % perl -MO=Concise -e 'print qq{hello, } . $world . qq{\n}' a <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 9 <@> print vK ->a 3 <0> pushmark s ->4 8 <2> concat[t4] sKS/2 ->9 6 <2> concat[t2] sK/2 ->7 4 <$> const(PV "hello, ") s ->5 - <1> ex-rv2sv sK/1 ->6 5 <$> gvsv(*world) s ->6 7 <$> const(PV "\n") s ->8 -e syntax OK

    The above both amount to much the same thing. So basically, you can always use double quotes, interpolate when you need to, and do not worry about performance.

    • another intruder with the mooring in the heart of the Perl

      You still need to take into account the complile time (if that is the correct terminology). But it does not seem that there is any real difference there either.
Re: Use of single quotes versus double quotes
by chromatic (Archbishop) on Dec 08, 2006 at 19:13 UTC

    Which book is that? If it really says that, I want to get it fixed. (It should say "clarity's sake" instead.)

    By the way, there are almost no huge time savings in micro-optimizations when you're doing IO.

      My apologies... I must have misread something. I just went through my Perl books and couldn't find anything. There was SOME reference to preferring single quoted strings over double quoted strings. But I can't find it... I think I may have imagined my own reasons for doing this: "oh, single quoted strings don't have to find the values of variables" Well, shoot, here I was going through and changing my doubles to singles thinking I was getting huge time savings
        There is no runtime difference unless there is a variable being interpolated, in which case only double quotes are appropriate. The perl parser treats them the same otherwise:
        perl -MO=Deparse -e ' $a = "foo"; $b = ''foo''; $c = q{foo}' $a = 'foo'; $b = 'foo'; $c = 'foo';
        perl -MO=Deparse -e ' $a = "$foo"; $b = q{$foo};' $a = "$foo"; $b = '$foo';
        "Perl Best Practices", by Conway. Chapter 4. "Use interpolating string delimiters only for strings that actually interpolate." I think the reasoning there is so that you don't unexpectedly get interpolation when you don't expect it. IMO, it's easier to always expect interpolation, as more often than not it's desired, and use single quotes only when it's clearer.
      I remember reading something along these lines in Programming Perl, 2nd edition. It was in a section near the end that listed tips on optimization. (I'm not saying the book was wrong, maybe it was just worded in a way that led me to the same misunderstanding as lokiloki.)

      ... I just looked at the 3rd edition in my safari bookshelf, and I see that it's chapter 24.2, Efficiency, that I'm thinking of -- but I don't see anything about quoting in this current edition.

      Joe

Re: Use of single quotes versus double quotes
by madbombX (Hermit) on Dec 08, 2006 at 19:49 UTC
    With everything else, you have to take this with a grain of salt, but I did what my favorite past time is when I want to see if one thing is faster than another and I Benchmark'd it. The difference is in the buffering since there is output being sent to a filehandle. I ran this about 10 times and these are the average results. (Most of the output has been removed for brevity). So using the following code, I got the following results:
    use Benchmark qw(cmpthese); my $tests = -25; sub single { print 'This is a test'; } sub double { print "This is a test"; } cmpthese($tests,{ single => \&single, double => \&double }); __OUTPUT__ Rate single double single 1952135/s -- -16% double 2313034/s 18% --

    As has been pointed out in other posts, the difference is nominal.

      18% slowdown is 'nominal'? Seems significant to me.
      If I switch the lines definig the subs, single becomes faster.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://588670]
Approved by idsfa
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (13)
As of 2014-12-18 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (51 votes), past polls