Regular Expressions Performance
-
I'm creating a new program. The ONLY thing which is important for me is the running time of the program. Should I use a regular expressions, or maybe should I write my own search code?
Without knowing what you are trying to achieve, it's hard to give any answers. I will say that a well designed regular expression will generally be faster than a roll your own search and replace algorithm, but a badly designed one can really kill performance when applied to large a data set. If, however, your search/replace doesn't naturally fit into a regular expression don't worry about it. Far too many times, people will contort themselves to write hugely complex regular expressions when a simple
string.Replace
would have sufficed."WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
I'm creating a new program. The ONLY thing which is important for me is the running time of the program. Should I use a regular expressions, or maybe should I write my own search code?
A regex may win when it involves a complex operation that fits well with a regex approach; OTOH most of the time regexes are both more cryptic and slower than a few explicit string operations. I once performed this experiment[^]. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
I'm creating a new program. The ONLY thing which is important for me is the running time of the program. Should I use a regular expressions, or maybe should I write my own search code?
I'd say write your program whichever way suits you. Then if performance is a problem profile it to find out where it is spending time. Then, and only then, think about how to speed it up. As has been pointed out, regexes may be faster, then again they may be slower.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.