Rather than speculate on efficiency you can always use benchmark.
One small point though - whenever you interpolate a string into a m// regex or the first half of a s/// regex
you need to backslash your special regex metacharacters $^*()+{[\|.?
The easiest way is to use quotemeta.
foreach $key (keys %hash) {
$key = quotemeta $key;
$text =~ s/$key/$hash{$key}/eg;
}
This is *vital* for reliability. Otherwise you will get unexpected runtime failures when your data eventually contains metachars (typos, deliberate, malicious...)
use Benchmark;
timethese(10000, {
'Simple loop' => '
$text = "This is my test string";
%hash = qw(test text foo bar use loop the end);
foreach $key (keys %hash) {
$key = quotemeta $key;
$text =~ s/$key/$hash{$key}/eg;
}
',
'Alternation' => '
$text = "This is my test string";
%hash = qw(test text foo bar use loop the end);
$regex = join("|", map(quotemeta, keys %hash));
$text =~ s/\b($regex)\b/$hash{$1}/eg;
',
}
);
Output:
Benchmark: timing 100000 iterations of Alternation, Simple loop...
Alternation: 5 wallclock secs ( 4.12 usr + 0.00 sys = 4.12 CPU) @ 2
+4271.84/s (n=100000)
Simple loop: 10 wallclock secs ( 9.72 usr + 0.00 sys = 9.72 CPU) @ 1
+0288.07/s (n=100000)
So as it happens alternation is twice as fast. I thought your solution would be faster but there you go!
If in doubt - use Benchmark
Cheers
tachyon
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.