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
| [reply] [d/l] [select] |
|
| [reply] |
|
Hey, that "perl -MO=Deparse .. " is something new for me! Nice, and thanks! Good to know about... like in cases like this!
| [reply] |
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.
| [reply] |
|
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.
| [reply] |
|
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.
| [reply] [d/l] [select] |
|
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.
| [reply] |
|
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. | [reply] [d/l] |
|
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 | [reply] [d/l] |
|
|
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
| [reply] [d/l] [select] |
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. | [reply] |