XML technologies / Regular expressions / Perl / Efficiency in Perl / The study function
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Unlike optimizations of the regex , study(...) examines the subject string more closely, trying to derive information useful for optimization. One or more regexes can use it when applied to that string. Typical usage looks like this:
while (<>) {
study($_); # Der Standardsuchstring $_ wird genauer untersucht.
if (m/Regex 1/) { ... }
if (m/Regex 2/) { ... }
if (m/Regex 3/) { ... }
if (m/Regex 4/) { ... }
}The operation of study is simple, but judging when study helps is harder. It never changes program results; the program merely uses more memory and runs slower, equally fast, or faster—the goal of using study .
When a string is processed with study , Perl invests time and memory to build a list of its characters and their positions. On most systems, the list is four times the string’s size. A regex can use it only while the string remains unchanged. Any modification invalidates the list, and processing another string with study overwrites it.
The benefit of preprocessing depends strongly on the regex and its optimizations. Searching for a literal string such as m/foo/ can make an enormous difference, up to a factor of 10,000 for very long strings. But using the /i modifier eliminates this improvement. In the current implementation, /i prevents all study benefits, along with other optimizations.
When should study be avoided?
Do not use
studyfor strings that will be searched with the/imodifier, or when every literal regex element is affected by˹(?i)˼or˹(?i:...)˼. These eliminate every advantage ofstudy.Do not use
studyfor short strings. The ordinary optimization Concatenating literals should suffice. What counts as short? Only experimenting with your own data and your own regexes can really answer. As a rough rule, I never usestudyunless the string is at least a few kilobytes long.The overhead of
studyis not worthwhile for only a few matches, or if the string is immediately modified. Gains are most likely when many searches recover the preprocessing cost. For only a few searches, the overhead ofstudyexceeds the matching savings.Use
studyonly for strings searched by regexes with exposed literal elements (see Exposing literal text).studyhelps only if a character required in every match can be found. One might thinkstudycould also improve theindexfunction, but it does not.
Where does study really help?
study is useful when a large string is searched repeatedly without modification. A good example is the filter I used to prepare this book. I wrote it in a homemade language translated by the filter into SGML, then converted to troff and finally PostScript. The filter turns each chapter into one string, about 475 kilobytes for this chapter. Many tests search for invalid formatting without changing the text. Most look for literal strings: exactly the situation suited to study .
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