[boulder.pm] line/loop optimization?

rise rise at knavery.net
Tue May 8 12:06:51 CDT 2001


On Tue, 8 May 2001, Robert L. Harris wrote:

> @Keys=keys(%ConfigOptions);
> while(<TemplateIn>) {
>   chomp;
>   $Line=$_;
>   foreach $i (@Keys) {
>     $Line =~ s/$i/$ConfigOptions{$i}/;
>   }
> }
>
>
> Is there a better way to do a line by line substitution instead of looping
> through @Keys each line?

Three immediate (speed, not necessarily maintainability) optimizations
occur to me:
a) If each Option is a fixed string, don't check each one individually.
   Instead do something like /(Option1|Option2|Option3/$ConfigOptions{$1}/
   since the match will contain your hash key for a fixed string match.
b) Use the qr{} quoting operation to get a "compiled" regex so that you're
   not reinterpolating for each line[1].
c) If values only change between runs you can use the /o modifier to make
   sure perl doesn't recompile the regex.

Basic strategy (untested):

@Keys=keys(%ConfigOptions);
# this next bit can be done in one operation
$OptionRegexString = join('|', @Keys);
$OptionRegexRef =  qr/$OptionRegexString/o;

while(<TemplateIn>) {
  chomp;
  $Line=$_;
  $Line =~ s/($OptionRegexRef)/$ConfigOptions{$1}/;
}


[1] perldoc perlop, in the section "Regexp Quote-Like Operators"
-- 
Jonathan Conway 	   The thing about Unix is that all the hoops are
rise at knavery.net           flaming, so at least you know where they are...





More information about the Boulder-pm mailing list