RegEx Performance Options

It's obviously not new, but the January 2006 edition of MSDN Magazine has some useful performance tips, particularly in the area of Regular Expressions. File this under the category of 'things I talk about here so I can refer to them myself later'.

A few quick quote snippets:

In the Regular Expression (Regex) space, there is the option to specify that a regular expression is compiled through use of the RegexOptions.Compiled setting. This switch hints at an underlying aspect of regular expressions in .NET: they have three modes.

Interpreted Regexes have the smallest impact to the startup performance of an application, but they have the slowest run-time speed since all of the processing remains to be conducted at run time. They are a good option if your Regex will be used rarely, since a rarely used expression won't impact overall run-time speed much (by definition), and taking any kind of startup hit probably isn't worth it

Regex precompilation solves many of the problems associated with compiling on the fly, and retains all of the performance benefits. Precompilation means you do all of the work of parsing and generating MSIL when you compile your app, ending up with a custom class derived from Regex. Run-time performance of this technique is identical to what you get when you compile on the fly.

There is, of course, a trade-off: the whole meaning of precompiled is that the Regex is put into its own assembly before you run your application. You therefore can't have the code for constructing the Regex inline, and you'll have to write other code (a tool perhaps) to construct your precompiled Regex ahead of time.

It's worth a read if you use a lot of Regular Expression, and in particular if you've ever had to use them inside loops.

Link: Base Class Library Performance Tips and Tricks: Regular Expression Compilation


Posted on Friday, April 13, 2007 10:23 AM | ASP.NET Web Development Miscellaneous

Like this? Share it!

  • # RegEx Performance Options
    Commented on 4/13/2007 11:54 AM

    Hmm... regex is from the dirty unix world.. unix is the precursor to Linux.. Linux is dirty free comunist code... You must be a dirty communist !! God damn Communists !!

    If no one is paying for it it's not good !!! 10 10

    nb. the views stated in this post may or may not be the views of the author of the post... 02

  • # RegEx Performance Options
    Commented on 4/13/2007 12:26 PM

    Also the author of that post might well be on crack :p

    (damn, where's the crack smoking smiley when you need him?)

  • # RegEx Performance Options
    Commented on 4/13/2007 12:48 PM

    If said author was on crack his rail would probably be more accurate !! but he'd still disslike regex !

Post a comment
Please add 2 and 3 and type the answer here:
Remember me?
Ensure the word in this box says 'orange':