[LA.pm] C-style Perl vs. regex

Kevin Scaldeferri kevin+lapm at scaldeferri.com
Wed Feb 8 14:33:53 PST 2006


On Feb 8, 2006, at 2:22 PM, Benjamin J. Tilly wrote:

> "Peter Benjamin" <pete at peterbenjamin.com> wrote:
> [...]
>> One optimization to use in your perl code is to get rid of
>> join for concatenation.
>>
>>>        my $zone = join "", $domain, ".zone";
>>>        my $hosts = join "", " mail.", $domain, " www.", $domain;
>>
>> Best is:
>>
>> my $zone = "$domain.zone";
>>
>> which is fastest and more readable.
>
> Benchmarks, please.
>
> I'll bet that you have a <b>very</b> hard time demonstrating a 
> noticable saving.


But, it's much more idiomatic.  The join "" thing makes you have to 
stop and think about whether there's something non-obvious going on.


>
>> Do not use the concat operator of period, '.', as according
>> to Larry, in chapters on perl optimization I have read,
>> that period operator is slow.  The only time I use it is
>> when I have to include an array or hash in a concatenation
>> and the value is not first available in a temporary variable.
>> For example:
>
> Which chapters, where?  It is true that building up a long string 
> using:
>
>   $foo = $foo . "new stuff";
>
> is slow - that is why .= exists.

I don't even think that's true any more.  I suspect that that 
particular case is optimized now.


>
>
> Remember that there are a lot of people who talk about optimization 
> out there, and most of them spout crap.  When in doubt, benchmark.

Absolutely.

Particularly in the case of things to do with string manipulations, 
perl is a lot more optimized behind the scenes than people seem to give 
it credit for.  Which is a little odd, because string manipulation is 
exactly what people use perl for the most.


>
> Incidentally IIRC, interpolation is converted behind the scenes into 
> concatenation.
>

Indeed as this makes fairly clear:

 > perl -wle 'my $a; print "a is $a"'
Use of uninitialized value in concatenation (.) or string at -e line 1.
a is


-kevin



More information about the Losangeles-pm mailing list