On Tue, Nov 27, 2001 at 09:55:02AM -0500, Andrew Stanley wrote:
> I wrote a bit of code that I was concerned about the speed of. It looks
> like:
>
> return map {
> my $x = $_;
> $x =~ s/(?<!^)(')(?!$)/$1$1/g;
> $x;
> } @_;
>
> So, I benchmarked it, and got:
>
> Benchmark: timing 1000000 iterations of lookaround...
> lookaround: 0 wallclock secs ( 0.48 usr + 0.00 sys = 0.48 CPU) @
> 2083333.33/s (n=1000000)
>
> I thought the (?<!) and (?!) were expensive, since I need to stop, look
> behind, look ahead, and then move on -- am I wrong? The actual
> benchmarking code follows.
I wouldn't expect lookahead or lookbehind to be significantly slower than a
normal (and unoptimized) part of a regex. All the comparisons are the
same; the only differences are where in the target string the comparison
occurs, and what is done with the result.
> timethese(1000000, {
> lookaround => q{
> map {
> my $x = $_;
> $x =~ s/(?<!^)(')(?!$)/$1$1/g;
> $x;
> } @arr;
> }
> } );
You haven't shown how @arr is declared and initialized. This is important,
because if @arr was declared as a lexical with my, then the code snippet is
operating on an empty array. If that's the case, you should either use a
global @arr or change the code snippet from a string to an anonymous sub.
Ronald
P.S. Instead of escaping the single quotes yourself, you should probably
be using placeholders or $dbh->quote(). :)