Ok so japhy told you why that is slow, here's a way to make your code fast regardless - don't even bother with the capturing.
Capturing is always slow because it has to make a copy of the source string. $1, internally, is just substr( $safe_copy_of_match, $-[1], $+[1] - $-[1] ). So the largest speed hit (that I'm aware of) is the memory operation of making a safe duplicate of the data that was just matched. COW (copy on write) may mitigate this if/when it ever gets into perl.
Likely to be be fastest. This was my second thought.
my $whatever_index = index lc $text , $whatever; return( substr( $text, 0, $whatever_index ), substr( $text, $whatever_index + length $whatever ) );
This may be the fastest. It was my third thought.
my $whatever_index = index lc $text, $whatever' ; my $whatever_length = length $whatever; return unpack "a" . $whatever_index . "x" . $whatever_length . "a*", $ +text;
This was my first thought. Use a plain regex to *locate* the thing in the string and then just substr() the equivalent of the captures out. This happens to be simplest to look at so it wins on the visual-complexity scale. This is a great general technique to avoid capturing on regexes and as such is a great post-bechmarking optimization.
if ( $text =~ /whatever/i ) { return( substr( $text, 0, $-[0] ), substr( $text, $+[0] ); }
In reply to Re: The Deceiver
by diotalevi
in thread Why does a Perl 5.6 regex run a lot slower on Perl 5.8?
by perldeveloper
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |