Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by Anonymous Monk
on Feb 02, 2005 at 09:47 UTC ( [id://427189]=note: print w/replies, xml ) Need Help??


in reply to To Single Quote or to Double Quote

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.

Replies are listed 'Best First'.
Re^2: To Single Quote or to Double Quote: a benchmark
by bass_warrior (Beadle) on Feb 02, 2005 at 14:45 UTC
    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%     --
    

      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.

      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.

      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?

Re^2: To Single Quote or to Double Quote: a benchmark
by saberworks (Curate) on Feb 02, 2005 at 18:59 UTC
    I think we need a benchmark with longer strings.
Re^2: To Single Quote or to Double Quote: a benchmark
by NateTut (Deacon) on Feb 02, 2005 at 20:07 UTC
    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 15:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found