XML technologies / Regular expressions / Perl / Efficiency in Perl
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Most Perl efficiency considerations apply to any traditional NFA. The techniques in The art of writing regular expressions, including internal optimizations, loop unrolling, and especially Think!, apply equally to Perl.
Perl also has specific characteristics. This section examines:
There’s More Than One Way To Do It
, TMTOWTDI. Perl offers many tools for solving a problem. Choosing suitable tools and approaches—the Perl Way—produces efficient, understandable programs. Efficiency and readability sometimes seem opposed; a better understanding of the language helps find a good solution.
regex compilation, /o, qr/.../, and efficiency
Variable interpolation and regex compilation can offer major savings. The modifier
/o, barely discussed so far, works with regex objects (qr/.../) to control when recompilation is necessary and when it is wasted work.
Penalties from $&
The three side-effect variables
$,$&and$'are convenient but penalize every regex in any script mentioning them. They need not actually be used ; merely appearing is enough.
The function study
Perl has long offered the function
study(...)to speed up regexes, but nobody seems to understandstudy. Let us see whether we can get to the bottom of it.
Benchmarking
The fastest program finishes first—obvious but true. Whether testing a fragment, function, or full program with real data, only benchmarks reveal actual speed. Perl makes benchmarks easy, though several approaches exist. I present a simple method that served me well hundreds of times while preparing this book.
Regex debugging support in Perl
Debugging options and pragmas ask Perl to report some optimizations performed by the regex engine and transmission. We will analyze the output to discover Perl’s secrets.
There’s More Than One Way To Do It
A problem often has many solutions. Balancing efficiency and readability may require trying them all. Consider zero-padding an IP address such as ›18.181.0.24‹ so every byte has three digits: ›018.181.000.024‹. A simple, understandable solution is:
$ip = sprintf("%03d.%03d.%03d.%03d", split(/\./, $ip));That works, but other solutions certainly exist. The table lists several, ordered by relative runtime with the fastest normalized to one. This simple task represents many similar text-processing problems. Compare the solutions; you may discover unfamiliar Perl constructs.
Table: Different ways to zero-pad an IP address.
Rank | Runtime | Approach |
|---|---|---|
1. | 1 |
|
2. | 1,3 |
|
3. | 1,6 |
|
4. | 1,8 |
|
5. | 1,8 |
|
6. | 2,3 |
|
7. | 3,0 |
|
8. | 3,3 |
|
9. | 3,4 |
|
10. | 3,4 |
|
11. | 3,4 |
|
12. | 3,4 |
|
13. | 3,5 |
|
14. | 3,5 |
|
15. | 3,6 |
|
16. | 4,0 |
|
All examples correctly handle well-formed IP addresses but fail differently on invalid ones, so validate separately if necessary. Otherwise, differences concern efficiency and readability. Examples 1 and 13 are readable, surprisingly with very different speeds. Examples 3 and 4, like 1, and 8, like 13, are reasonably clear. The rest look cryptic.
Why are some faster? Several factors matter: NFA operation (How regex engines work), optimizations (The art of writing regular expressions), and efficiency of other Perl constructs, here sprintf and substitution. Solutions using the /e modifier mostly appear near the bottom.
Pairs 3/4 and 8/14 differ only in regex parentheses; omitting parentheses is slightly faster. But example 8 replaces parentheses and $1 with the variable $&, which can severely slow other regexes in the program (see Perl copies the subject string before matching), though apparently not here.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln