in reply to Re^2: Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ???
in thread Regex combining /(foo|bar)/ slower than using foreach (/foo/,/bar/) ???
But if you are so heavily rewriting a string, perhaps it is time to rethink your algorithm. One problem might be that the engine needs to search through the whole string for every search and replace pattern and that search tests the pattern at every position in the string. If you know that the string has a lot of structure and that a given pattern could only match at certain places, attempting a match at every position is a lot of wasted effort.
You mentioned XML; if you are replacing particular attributes or elements, it may be a whole lot faster to parse the file using XML::Parser with the lightwieght SAX interface, replacing things as you go. That way the string is only traversed once and replacement becomes, e.g., a simple hash lookup.
Update: added a phrase.
-Mark
|
|---|