Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: To Single Quote or to Double Quote: a benchmark

by eric256 (Parson)
on Feb 02, 2005 at 17:08 UTC ( #427334=note: print w/replies, xml ) Need Help??


in reply to Re^2: To Single Quote or to Double Quote: a benchmark
in thread To Single Quote or to Double Quote

A quick change to your benchmark measures more accurately what we are talking about. Assume the evals and string qoutes are right because you lost me in there somewhere.

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.


___________
Eric Hodges

Replies are listed 'Best First'.
Re^4: To Single Quote or to Double Quote: a benchmark
by Anonymous Monk on Feb 03, 2005 at 10:05 UTC
    You see a consistent win for single quotes,
    Actually, I don't. In 2 out of the 5 benchmarks, I see "double no $" beat "single no $".

    The fact that "double" is slower than "single" comes partially because "single" is one character shorter than "double" (if I add a single space to "single", it takes about 3% longer), and for the rest because processing a backslash in a double quoted string means checking more cases than in a single quoted string.

      If you read my post you'll see I added the "no $" after the original post. Ignoring those cases the single quote wins. You can blame the extra character if you want but i beleive that is the whole point here. If you use double quotes then you have to escape characters and it makes a noticable difference in speed. You should also have noticed that I mentioned the speed gain is minimal at very best. There is no reason at all to use these speed tests in a design choice.

      for the rest because processing a backslash in a double quoted string means checking more cases than in a single quoted string. Isn't that realy the point that I was showing. Double quotes ARE slower because of the restrictions they impose, but they are only slower if it actualy contains something that needs escaped. Is that speed difference important? I would say not in normal use, but maybe for some special case.


      ___________
      Eric Hodges
        Double quotes ARE slower because of the restrictions they impose, but they are only slower if it actualy contains something that needs escaped.
        Indeed, but let's go back to where it all started, shall we? Here's what the OP wrote:
        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.
        Which to me means, the OP is talking about strings which don't have characters that are special - where the syntax of having single or double quotes doesn't matter. Obviously, if you have scalar or array sigils, or backslashes, the special properties of single or double quotes do matter - and are not the occasions we are talking about.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://427334]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2021-10-25 15:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (89 votes). Check out past polls.

    Notices?