performance Re: string not block Re: SPUG: interpolating into a
string variable
Fred Morris
m3047 at inwa.net
Sun Dec 21 01:23:30 CST 2003
Interestingly, my brain-dead workaround seems to perform slightly better
than eval for a smallish number of substitutions; it also doesn't kick
errors when a variable isn't defined (it just leaves it alone). (8 seconds
versus 10 seconds)
#!/usr/bin/perl -w
use strict;
my %subs = ( foo_str => 'foo', bar_str => 'bar', baz_str => 'baz');
my $source = '$fizz boom $foo_str bim bam $baz_str';
print "\nready>";
my $line = <>;
for (my $i = 0; $i < 20000; $i++) {
my $s = $source;
foreach my $key (keys %subs) {
$s =~ s/\$$key/$subs{$key}/;
}
}
print "\ndone>";
$line = <>;
exit(0);
versus:
#!/usr/bin/perl -w
use strict;
my $foo_str = 'foo';
my $bar_str = 'bar';
my $baz_str = 'baz';
my $source = 'fizz boom $foo_str bim bam $baz_str';
print "\nready>";
my $line = <>;
for (my $i = 0; $i < 20000; $i++) {
my $s = eval ' my $s = "' . $source . '";';
}
print "\ndone>";
$line = <>;
exit(0);
FWIW...
--
Fred Morris
fredm3047 at inwa.net (I-ACK)
More information about the spug-list
mailing list