Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Differences between " " and ' '?

by Ace128 (Hermit)
on Jul 31, 2005 at 02:55 UTC ( [id://479677]=perlquestion: print w/replies, xml ) Need Help??

Ace128 has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks,

This time I have a more easier question. Now, doing alot of string comparisons:
my $text = "pomperipossa"; if ($text eq "pomperipossa") { .. }
I was wondering if there is _any_ speed difference between doing:
if ($text eq "pomperipossa") { .. }
and:
if ($text eq 'pomperipossa') { .. }
?
Would also be nice if you experts could elaborate more on this. If some cases are better with "" than with ''... (yea, I know about the difference when it comes to $variables! so it seems that ' ' would be faster! Here all is just outputed as it is, compared to " "! But is that the same for eq, ne etc?)

Thanks,
Ace

Replies are listed 'Best First'.
Re: Differences between " " and ' '?
by saintmike (Vicar) on Jul 31, 2005 at 03:09 UTC
    If there's no variables inside to be interpolated, the optimizer transforms double quotes to single quotes:
    #!/usr/bin/perl #testscript if($a eq "quackidiquack") { } if($a eq 'quackidiquack') { }
    Now let's see what the optimizer does:
    perl -MO=Deparse testscript if ($a eq 'quackidiquack') { (); } if ($a eq 'quackidiquack') { (); } testscript syntax OK
      Your conclusion is correct but your methods are dodgy. B::Deparse doesn't tell you anything about how things are compiled or executed internally and it can be incorrect or just misleading. For accuracy, use B::Concise or something else that shows you the compiled optree. B::Deparse attempts to produce some sensible perl code based on the optree but it isn't really an oracle and it isn't always right.
      Added

      B::Deparse is supposed to produce canonically correct output as a core goal and it does really well. It just isn't so perfect. It also won't tell you that '...' is internally the same thing as "...". (it is)

      Hey, that "perl -MO=Deparse .. " is something new for me! Nice, and thanks! Good to know about... like in cases like this!
Re: Differences between " " and ' '?
by Zaxo (Archbishop) on Jul 31, 2005 at 03:08 UTC

    Because it need not look for interpolation, single quotation is somewhat faster for literal strings. The difference is unlikely to amount to much in the run of a program.

    It's good practice to observe the difference and use single quotes when they will do. That tells readers that interpolation is not expected.

    After Compline,
    Zaxo

      See A better non-existant string... for how incredibly little it matters performance-wise.

      I recommend you use single quotes if you think you are likely to add $ or @ to the string in future and not want interpolation and use double quotes if you think you are likely to want interpolation in future. Otherwise, it so doesn't matter.

      - tye        

        D:\perl> perl -MBenchmark -e "timethese( 1000000, { doubles => sub { my $interpolated = \"pomperipossa\" }, singles => sub {my $uninterpolated = 'pomperipossa' } } )"

        Gives these results:

        Benchmark: timing 1000000 iterations of doubles, singles...

        doubles:  0 wallclock secs ( 0.46 usr +  0.00 sys =  0.46 CPU) @ 2169197.40/s (n=1000000)

        singles:  0 wallclock secs ( 0.45 usr +  0.00 sys =  0.45 CPU) @ 2217294.90/s (n=1000000)

        So in this (highly unrealistic) case, singles seem infinitesimally faster. I use singles for things I think will be constant, and doubles only when I actually want interpolation. But this is nothing to do with alleged speed benefits, and everything to do with making my intentions clear.

Re: Differences between " " and ' '?
by Tanktalus (Canon) on Jul 31, 2005 at 14:08 UTC

    A Super Search pulled up a thread earlier this year where not only was this benchmarked, but some of the meta-questions where talked about, such as when to use which.

      That was an interesting thread. Base on those numbers, double and single quote really does not have much speed difference and the difference is not consistent. bass_warrior's testing was meaningless, he made up cases to drive a pre-determined conclusion.

      I slightly modified the first set of benchmark provided by Anonymous Monk, and when the string is a little bit longer, the chance of winning between double and single quote becomes really close to 50-50:

      use warnings; use Benchmark("cmpthese"); cmpthese(-1, { "double", q !eval q {$a = "this is a string" x 10}!, 'single', q !eval q {$b = 'this is a string' x 10}!, });

      Conclusion: the difference between single and double quote are more functional than performance.

      Update: when I tried x 100, double seems to win more. In a way, it shows that length of the string matters here. The overall conclusion stays, the speed difference does not mean much.

        Your post seemed to be incorrect. I don't believe you are interpreting the output in the right fashion. The double still shows up as being FASTER, abnormally faster with your example.

        use warnings; use Benchmark("cmpthese"); cmpthese(-1, { "double", q !eval q {$a = "this is a string" x 10}!, 'single', q !eval q {$b = 'this is a string' x 10}!, });

        Not just did it show a speed increase but 46%, a quite significant one:

        Rate double single
        double 39422/s -- -31%
        single 57420/s 46% --


        Evan Carroll
        www.evancarroll.com
Re: Differences between " " and ' '?
by bradcathey (Prior) on Jul 31, 2005 at 11:43 UTC

    Related to speed issues, what about double-quotes/single-quotes VS.

    qq/pomeripossa/

    OR

    q/pomeripossa/

    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: Differences between " " and ' '?
by Anonymous Monk on Aug 01, 2005 at 11:23 UTC
    For strings where it matters whether I use single or double quotes, I use single or double quotes were appropriately. For strings were it doesn't matter - strings that don't contain quotes, backslashes or dollar/at signs, I tend to use double quotes. I do that because it lowers my expected number of keystrokes I need when I'm going to change the string. I'm far more likely to add a newline or an interpolated variable to a string than to add a backslash, or a dollar sign that I don't want to be interpolated. If my quotes are already double quotes, I just add my newline or variable, and be done. Else, I have to add the newline or variable, go the beginning of the string, change the single quote to a double one, and do it at the end of the string as well. In a typical program I write, for the majority of the strings it doesn't matter whether I use single or double quotes - but for the strings were it doesn't, those needing double quotes outnumber those that don't by a long shot. To top that off, I can always escape a dollar/at sign inside double quotes by inserting a single character - single quotes don't have an easy way of indicating a dollar/at sign should interpolate.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://479677]
Approved by Zaxo
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found