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

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

by bass_warrior (Beadle)
on Feb 02, 2005 at 14:45 UTC ( [id://427271]=note: print w/replies, xml ) Need Help??


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

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%     --

Replies are listed 'Best First'.
Re^3: To Single Quote or to Double Quote: a benchmark
by eric256 (Parson) on Feb 02, 2005 at 17:08 UTC

    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
      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
Re^3: To Single Quote or to Double Quote: a benchmark
by Tanktalus (Canon) on Feb 02, 2005 at 17:14 UTC

    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.

Re^3: To Single Quote or to Double Quote: a benchmark
by Anonymous Monk on Feb 02, 2005 at 16:58 UTC
    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?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-09-08 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.