pegasus has asked for the wisdom of the Perl Monks concerning the following question:

Hi.

I was reading merlyn's article on multi-page CGI forms and have a question on string delimiters.

Would there be a significant benefit to replacing most of his uses of double quote delimiters with single quotes? As far as I can tell, there's not a lot of interpolation needed in the definition. I can see where a few would need double-quotes, but most seem not to.

I'm hoping to adapt the article for a project I've been given and hope to make it as fast as possible.

Replies are listed 'Best First'.
Re: String Delimiters
by tadman (Prior) on Apr 13, 2001 at 20:17 UTC
    Presumably, the only thing that the double quotes would slow down is in the parse/compilation phase. After that, it is unlikely that "x" and 'x' would process any differently.

    "As fast as possible" means Benchmark, of course:
    Benchmark: timing 25 iterations of Double, Single... Double: 2 wallclock secs ( 1.57 usr + 0.01 sys = 1.58 CPU) @ 15 +.82/s (n=25) Single: 1 wallclock secs ( 1.57 usr + 0.01 sys = 1.58 CPU) @ 15 +.82/s (n=25) Rate Double Single Double 15.8/s -- -0% Single 15.8/s 0% --
    Which seems to be bang on. If you introduce '$' into the characters that are used to create the random strings, there is a slight difference:
    Benchmark: timing 25 iterations of Double, Single... Double: 3 wallclock secs ( 3.13 usr + 0.14 sys = 3.27 CPU) @ 7 +.65/s (n=25) Single: 3 wallclock secs ( 3.18 usr + 0.06 sys = 3.24 CPU) @ 7 +.72/s (n=25) Rate Double Single Double 7.65/s -- -1% Single 7.72/s 1% --
    So it would seem that using single or double quotes, apart from functional differences, is merely style. I prefer to use double quotes almost always, except for short single-letter strings, but that's the influence of C where single quotes can only be used for single characters, and double quotes meant 'string'.

    And the code for the curious:
    #!/usr/bin/perl use Benchmark qw [ cmpthese ]; my (@letter) = ('A'..'Z','a'..'z','0'..'9'); my (@single,@double); sub RandomCrap { return join ('', map { $letter[rand(@letter)] } 0..64); } sub Prep { for (my $i = 0; $i < 5; $i++) { my $func = '@x = ('; for (my $n = 0; $n < 1000; $n++) { $func .= "'".RandomCrap()."',"; } $func .= ')'; push (@single, $func); } @double = map { tr/'/"/; $_ } @single; } sub Evalu { my ($array) = @_; foreach my $func (@$array) { eval $func; } } Prep(); cmpthese (25, { 'Single' => sub { Evalu (\@single); }, 'Double' => sub { Evalu (\@double); }, } );
Re: String Delimiters
by indigo (Scribe) on Apr 13, 2001 at 20:54 UTC
    The performance difference will be minimal.

    Personally, I go with single quotes for readability, so I can quickly tell whether or not I have to hunt down interpolated variables. But that is more of a personal style issue.
Re: String Delimiters
by arhuman (Vicar) on Apr 13, 2001 at 21:01 UTC
    Tadman pretty said everything, for even more details see :
    Overuse of double quotes?

    To be convinced that there's NO difference as it translated at compile time into a single quoted string.


    "Only Bad Coders Badly Code In Perl" (OBC2IP)

      See also A better non-existant string... where I note:

      This means that if your script has four thousand 3200-byte strings enclosed in double quotes, then you could make it start up 0.2 seconds faster by changing all of those double quotes to single quotes. No matter how long the script ran, no further speed benefit would be achieved.

      which means that worrying about such things wastes more time than it saves for most people. (:

              - tye (but my friends call me "Tye")
Re: String Delimiters
by pegasus (Beadle) on Apr 13, 2001 at 22:24 UTC

    Thanks for the information.

    Someone sent me a message indicating that double quotes get converted to single quotes during compilation. That probably explains why there was no difference in performance.

    I'm having a couple of problems getting merlyn's example to work. As written, line 65 says print $CGI::Q->dump(), which my version of Perl and CGI don't seem to like.

    I replaced it with print CGI->Dump(); to call the subroutine declared at line 1013 of my copy of CGI.pm. When I run my code, nothing appears.

    Any ideas?