in reply to Regex speed issue

Your code is likely buggy. You've passed in read-only values to $this->mask_block_params(...) and you have no idea that $` and $' are still valid by the time you use them. Here's an alternate reformulation which also avoids incurring the $`, $&, $' speed penalties. If this is your only use of $`, $&, and $', you may find a nice speed improvement.

while ($this->{template} =~ /$re/) { # Uses substr and @-/@+ instead of $` and $'. Also is sure to capt +ure those values before executing other potentially regex-using code. my ( $prior, $start, $close, $after ) = = ( substr( $this->{template}, 0, $-[0] ), $1, $2, substr( $this->{template}, $+[0] ) ); my $params = $this->mask_block_params( $start, $close ); my $html = &$callback($key, $params); $this->{template} = $prior . $html . $after; }

Replies are listed 'Best First'.
Re^2: Regex speed issue
by mtsachev (Initiate) on Aug 30, 2005 at 15:23 UTC

    It doesn't hurt to pass ro variables to mask_block_params.

    All methods mentioned here require the same cpu time. That reminded me that the replace is not the slow part, matching is the slow one. So what I thought is get rid of html like attributes, this was a pretty good speed improvement. 12s to 1s.

    Looks like the second .*? is pretty expensive.

      Replaced the regex with HTML::PullParser, takes 0.3s now.