Any suggestions to tune this?
Cool. I can offer a suggestion that uses a module I wrote. Regexp::Assemble is designed to optimise expressions like this. Consider the following:
use Regexp::Assemble; my $r = Regexp::Assemble->new; while( <DATA> ) { chomp; $r->add( $_ ); } print $r->as_string; # produces ^(?:K[LM]|P[AM]|S[LZ]|CP|ME|WX|YZ)XX1 __DATA_ ^CPXX1 ^KLXX1 ^KMXX1 ^MEXX1 ^PAXX1 ^PMXX1 ^SLXX1 ^SZXX1 ^WXXX1 ^YZXX1
You can also do this as a one liner:
print Regexp::Assemble ->new( chomp=> 1 ) ->add( <DATA> ) ->as_string;
The other thing you can do is take the assembled expression, remove the ?: to turn it into a POSIX-compatible expression, and feed that to egrep. (see Aristotle's words of wisdom below).
update: hmm, I see that this produces the same thing that other posters have done by hand. Bear in mind that R::A is more at home when dealing with hundreds or thousands of discrete expressions: that is where it starts to shine. With 10 expressions it's like a car in first gear. Still, if your real-world environment is using more than you show here, you will see a definite improvement.
Another thing it's good at is when your expressions aren't so, um, regular. Consider what happens when PMXX1 is changed to PMXX2. (Hint: ^(?:(?:K[LM]|S[LZ]|CP|ME|WX|YZ)XX1|P(?:AXX1|MXX2))). That's not quite as easy to work out by hand.
- another intruder with the mooring in the heart of the Perl
In reply to Re: perl performance vs egrep
by grinder
in thread perl performance vs egrep
by dba
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |