On Jan 2, 2008 8:04 AM, Paul Johnson <[EMAIL PROTECTED]> wrote:
snip
> > 2) Static string Handling
> > use single quotes rather than doubles. Double quotes force Perl to look for
> > a potential interpolation of information, which adds to the overhead of
> > printing out the string.
> > Print 'I am a static string I don't want to get interpolated';
> > If we want to interpolate then do it as like this
> > Print 'I am a static string' , "\n" , 'I don't want to get interpolated';
>
> Who told you this?  I'd be surprised if you could measure any
> performance difference between using single and double quotes when there
> is no interpolation going on, and you certainly won't measure any
> *runtime* performance difference.
>
> Some people get religious about this as a matter of style.  Ignore them.
snip

I was working up an email with benchmarks to show how bad this advice
is (but got distracted by work), and there is in fact a difference in
the compile time for large strings (but as you point out, no
difference in run time).  The difference only really kicks in around
one thousand characters, and if you have a constant string that is a
thousand characters long, you have more pressing problems than the
minuscule speed boost during compilation.

#!/usr/local/ActivePerl-5.10/bin/perl

use strict;
use warnings;
use feature ':5.10';
use Benchmark;

my $s = "a" x 10;

#note: string interpolation costs for a constant string
#only effects compile time not run time, so we are forced
#to use the evil string based eval to measure it
my %subs = (
        single => sub {
                my $ret = eval qq('$s');
                return $ret;
        },
        double => sub {
                my $ret = eval qq("$s");
                return $ret;
        },
);

say "$_ => ", $subs{$_}() for sort keys %subs;

for my $n (10, 100, 1_000, 10_000, 100_000) {
        $s  = 'a' x $n;
        say "for a string $n characters long:";
        Benchmark::cmpthese(-1, \%subs);
}

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to