Re: To Single Quote or to Double Quote
by perrin (Chancellor) on Feb 01, 2005 at 20:29 UTC
|
This is a case of "say what you mean." If you mean "here is a literal string", use single quotes. If you mean "here is a string with some stuff I want you to interpolate", use double quotes. The performance difference is not enough to care about, but you should use the right one for clarity. | [reply] |
|
I agree. I just wonder about the performance hit on all those unnecessary "s
Doug
| [reply] |
|
| [reply] |
Re: To Single Quote or to Double Quote: a benchmark
by Anonymous Monk on Feb 02, 2005 at 09:47 UTC
|
Here's a benchmark, comparing the difference between a double quoted string, and a single quoted string:
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark("cmpthese");
cmpthese(-1, {
"double", q !eval q {$a = "this is a string"}!,
'single', q !eval q {$b = 'this is a string'}!,
});
And the results:
Rate single double
single 34133/s -- -1%
double 34462/s 1% --
Now, before you think double quotes are faster, the results of another run:
Rate double single
double 33810/s -- -0%
single 33811/s 0% --
When I ran this 10 times, double quotes won 5 times, single quotes won 4 times, and there was one tie. The difference was never more than 1%.
So my conclusion, on this single benchmark, is, performance wise, it doesn't matter. If you still want to claim there's a performance difference, come with either a better benchmark, or a discussion of the code of perl that shows there's a difference. | [reply] [d/l] [select] |
|
This got me to thinking so I took your original code and made a few modifications just to see what it would do.
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark("cmpthese");
my $i = 1;
cmpthese(-1, {
"double", q !eval q {$a = "this is a string $i"}!,
'single', q !eval q {$b = 'this is a string $i'}!,
});
seeker:perl$> ./quotes.pl
Rate double single
double 11712/s -- -34%
single 17742/s 51% --
seeker:perl$> ./quotes.pl
Rate double single
double 11487/s -- -30%
single 16377/s 43% --
seeker:perl$> ./quotes.pl
Rate double single
double 11270/s -- -32%
single 16592/s 47% --
seeker:perl$> ./quotes.pl
Rate double single
double 11395/s -- -33%
single 16905/s 48% --
seeker:perl$> ./quotes.pl
Rate double single
double 11894/s -- -33%
single 17742/s 49% --
seeker:perl$> ./quotes.pl
Rate double single
double 11487/s -- -32%
single 16853/s 47% --
| [reply] [d/l] |
|
use strict;
use warnings;
use Benchmark("cmpthese");
cmpthese(-1, {
'double' , q !eval q {$a = "this is a string \$i"}!,
'single' , q !eval q {$b = 'this is a string $i'}!,
'double no $', q !eval q {$a = "this is a string"}!,
'single no $', q !eval q {$b = 'this is a string'}!,
});
__DATA__
C:\test>perl interpolation_bench.pl
Rate double single double no $ single no $
double 22226/s -- -16% -17% -17%
single 26331/s 18% -- -2% -2%
double no $ 26733/s 20% 2% -- 0%
single no $ 26733/s 20% 2% 0% --
C:\test>perl interpolation_bench.pl
Rate double single double no $ single no $
double 21065/s -- -20% -21% -21%
single 26331/s 25% -- -1% -2%
double no $ 26714/s 27% 1% -- -0%
single no $ 26756/s 27% 2% 0% --
C:\test>perl interpolation_bench.pl
Rate double single single no $ double no $
double 21966/s -- -17% -18% -18%
single 26551/s 21% -- -1% -1%
single no $ 26733/s 22% 1% -- -1%
double no $ 26947/s 23% 1% 1% --
C:\test>perl interpolation_bench.pl
Rate double single single no $ double no $
double 22226/s -- -14% -16% -17%
single 25960/s 17% -- -1% -3%
single no $ 26331/s 18% 1% -- -2%
double no $ 26733/s 20% 3% 2% --
C:\test>perl interpolation_bench.pl
Rate double double no $ single single no $
double 20785/s -- -19% -21% -23%
double no $ 25551/s 23% -- -3% -5%
single 26211/s 26% 3% -- -3%
single no $ 27019/s 30% 6% 3% --
You see a consistent win for single quotes, I don't think it is enough to worry about though. If you are dealing with 100k strings and you want to decide if you should sinle or double quote them, then you probably made a bad choice to load 100k variables ;)
Update: Added benchmarks for strings with no $ in them. Looks like perl must do a prescan to see if it needs work or not. I could of course be completely crazy.
| [reply] [d/l] |
|
|
|
|
The way I read this benchmark is that for an approximately 50% gain in speed, you get the wrong answer.
All depends on what you're looking for ... but '$i' is probably not what the user wants. As is pointed out elsewhere, this is comparing apples and oranges - one of them is wrong, and generally that would be the single quotes.
| [reply] |
|
I fail to see what your point is. Now you're comparing two different things. In the single quote case, you're assigning a plain string to a variable - in the double quoted case, you're interpolating an undefined (or did you think a lexical variable in the main program is visible in the Benchmark package?) variable into a string, and assign that to a variable.
So, am I surprised that if you do more, it takes more time to run? Nope. Does it, in any way, invalidate the premisses that there's no performance difference between identical single quoted and double quoted strings? Nope, not in the least bit.
Tell us, what's the point of your post?
| [reply] |
|
I think we need a benchmark with longer strings.
| [reply] |
|
I can't believe someone actually wrote a benchmark for this! That is too cool or maybe you have too much time on your hands. Thanks though.
Doug
| [reply] |
Re: To Single Quote or to Double Quote
by jacques (Priest) on Feb 01, 2005 at 21:15 UTC
|
When it doesn't matter, I always use double unless I need single. When I am looking at thousands of lines of code, I prefer the obviousness of double quotes. Plus, eventually I might need to have the string interpolated, in which case I would have to change the quotes around if I use single. | [reply] |
|
Funny - I do it exactly the opposite. I use singles unless I need double, because then interpolating happens only when I say so, and I otherwise don't have to pay attention to sigils or other special characters. When interpolation happens, then I have to scan the line to figure out if I want to go and escape everything, or if I leave it as-is, and use the concatentation operator '.'. As far as obviousness, I suggest a syntax-highlighting editor. ;-)
| [reply] |
|
I second your recommendation to use a syntax-highlighting editor, but I want to expand. Not all editors (even fte, sadly, which I loved) handle complex quoting in Perl in a sane manner. For example, an editor might have trouble with some of:
qq[];
s///;
y///; ##and so on, any with three separators
s[][]; ##etc
m,/,; ##non-standard matching, esp with a slash
m/\//; #matching with an escaped version of a delimeter
The only editor I've ever found that handles all of those correctly is SciTE.
radiantmatrix
require General::Disclaimer;
s//2fde04abe76c036c9074586c1/; while(m/(.)/g){print substr(' ,JPacehklnorstu',hex($1),1)}
| [reply] [d/l] |
|
|
|
I also lean toward sticking with 's. For the "only interpolate when I really mean it" reason as well as my probably unfounded fear of a performance hit on "s.
Doug
| [reply] |
|
| [reply] [d/l] |
|
Remove the q? I prefer to use qq'blah' when I need interpolation.
;)
| [reply] |
Re: To Single Quote or to Double Quote
by Anonymous Monk on Feb 02, 2005 at 09:35 UTC
|
On occasions such as those when you don't care about the special properties of either single or double quotes, which is the best to use.
My hunch is that single would be better because there would be less processing of escaped characters.
On occasions where it doesn't matter whether you use double quotes or single quotes, there are no escaped characters, so no processing of escaped characters happens. If you mean looking for escaped characters, remember that with single quotes, the tokenizer still has to look for them, as the delimiters, and the escape character, still can be escaped.
As for which is the best, I'll tell you which is the best, if you can tell me the measurement you mean by "best". Single quotes use less ink when printed, perhaps that's a reason to find single quotes better. OTOH, a lot of printing software (specially printing software used in (bad) Perl books) mangle single quotes into something unrecognizable. Which would mean double quotes are better. Then there's C, which uses double quotes for strings, perhaps that makes double quotes better. But a double quote means I have to hit the shift key, but I don't need to for single quotes. Perhaps that makes single quotes better.
Oh well, perhaps we should reach the conclusion that "which is better" questions that don't come with a measurement are pointless.
| [reply] |
Re: To Single Quote or to Double Quote
by wolfger (Deacon) on Feb 02, 2005 at 17:01 UTC
|
Others will look at performance issues (or lack thereof). I look at the front-end of it. Using single quotes means I don't have to hit that shift key. Single quotes saves me work, so that's the only logical choice. :-)
On the other hand, using double quotes (in my opinion) makes the code more readable... So I guess it's still a toss-up.
--
Linux, sci-fi, and Nat Torkington, all at Penguicon 3.0
perl -e 'print(map(chr,(0x4a,0x41,0x50,0x48,0xa)))'
| [reply] [d/l] |
Re: To Single Quote or to Double Quote
by kprasanna_79 (Hermit) on Feb 02, 2005 at 14:33 UTC
|
Hey
The main difference u will see between single quotes and double quotes is
single quotes will escape all escape charater
double quotes will act as a string and prints what ever u give inside it except perl variables
--prasanna.k | [reply] |
|
| [reply] |
|
single quotes will escape all escape charater
Not completely:
print 'there\'s a \' quote here, there\'s a \\ backslash here';
Anyway, it works the other way around: single quotes interpret only a few escape sequences, double quotes have more (and they interpolate variables).
See the section on "Quote and Quote-like Operators" in perlop.
| [reply] [d/l] |