codebeater

General .NET, ASP.NET, C# and VB.NET discussion

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009

Use Precompiled or Inline Regular Expressions for improved performance

Standard Regular Expression usage in .NET is the interpreted variety.  In my experience "interpreted" typically means less-than-optimal performance.  The purpose of this post is to describe how to precompile or inline your regular expressions for better performance.  I'm not going to make the argument for or against regular expressions vs. handwritten parsing code, you can search for those answers elsewhere.

It probably doesn't make much sense to even worry about this when your regular expressions will be used only infrequently,  However, if you have expressions that are executed frequently, as in a loop or something, you may find that interpreted regular expressions can drag you down a bit.

Pros and Cons

  • Interpreted Regular Expressions start quickly but will not perform optimally at runtime.
  • Precompiled Regular Expressions require a little more start time but provide improved runtime performance.  If you have an expression that is executed frequently, you may want to consider this option.  I've read that precompiled regular expressions perform 30% improved performance over the interpreted variety.
  • Inline Regular Expressions creates an entirely new assembly that provides the benefits of improved start time as wells as the benefits of precompiled expressions.  Since a new assembly is created it can be reused across applications, which is always, IMO, a good thing.

In the following three examples I will show how to do each with a simple regular expression that checks for a valid email address.

  • Interpreted
public bool IsValidEmailAddress(string email)
{
    Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
    return regex.IsMatch(email);
}
  • Compiled
public bool IsValidEmailAddress(string email)
{
    Regex regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$",RegexOptions.Compiled);
    return regex.IsMatch(email);
}
  • Inline

Ok, this one's a little, but not much, more complicated.  The goal here is to create an assembly with your expressions built into it.  For my email validation expression we can do this:

 

RegexCompilationInfo regexInfo = new RegexCompilationInfo(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", RegexOptions.None, "EmailExpression", "MyRegEx", true);
Regex.CompileToAssembly(new RegexCompilationInfo[] { regexInfo }, new AssemblyName("MyRegEx"));

 

This will create an assembly named "MyRegEx.dll" in your Bin directory (see image below).  Then, from the application that will use this assembly you can add a reference to your new regular expression assembly.

precomp_regex

 

Making use of the new assembly is quite easy:

private bool IsValidEmailAddress(string email)
{
    MyRegEx.EmailExpression ee = new MyRegEx.EmailExpression();
    return ee.IsMatch(email);
}

That's it.  Pretty simple, eh?

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Posted by jeff on Sunday, April 27, 2008 4:52 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Tuesday, January 06, 2009 7:33 PM