Keep It Simple, Stupid | |
PerlMonks |
Re: Regex matching on grid alignmentby rjt (Curate) |
on Sep 08, 2013 at 23:16 UTC ( [id://1052937]=note: print w/replies, xml ) | Need Help?? |
I think you'll find this will do the trick:
The idea here is to minimize the branching the RE engine will have to do. The logic is pretty similar to what you might do if you had the string split into lines; just skip 0..$H1 rows, and we know we're at the beginning of a row, so from there we just match 0..$W-3 characters followed by a repeating sequence of 3 with your original regex. Performance is the same (a few % better actually) as the plain /(.)\1\1/, and several times faster than anything I tried with unpack or split. Edit: You can get another ~25% or so if your character set really is small like [ABCD] by unrolling (.)\1\1 into (?:AAA|BBB|CCC|DDD). If you're not just using this as a boolean test and still need the character in $1, use (AAA|BBB|CCC|DDD) instead and use substr($1,0,1) to grab the first character if you get a match. The idea here is to push the more expensive operations out of the hot loop that's called millions of times. ___________
use strict; use warnings; omitted for brevity.
In Section
Seekers of Perl Wisdom
|
|