in reply to Alternation vs. looping for multiple searches.
If you want to know which will be faster, then you need to benchmark. My gut feeling is that alternation will be faster than loops, but I could easily be wrong. There will also be details in exactly what you are searching for that will affect performance. For example, if your are looking for the words "fox", "fred" or "frodo", then the regular expression /f(r((odo)|(ed)))|(ox)/ might be faster than a simpler matcher for each word separately. On the other hand perhaps the perl regular expression engine will do that optimisation for you, so writing the more complex regex above will make difference.
One important tip, is that if the text you are searching for (and therefore the regular expression) is not know at perl compile time, then you can explicitly compile the regular expression at runtime, which will improve performace. Do that with "qr" brackets: my $compiled_re = qr/pattern/
You also asked which is better. That depends on a whole heap of factors, but if readability and maintainability is more important than performance, then forget what I just said, and write a simple algorithm that is easy for anyone to read and understand, even if it is a little slower.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Alternation vs. looping for multiple searches.
by JavaFan (Canon) on Nov 21, 2010 at 14:44 UTC |