Thursday, August 20, 2009

Increasing performance (.NET Regular Expressions)

I just discovered that the .NET Regex class offers a static method (CompileToAssembly) to compile a regular expression to an assembly for increased performance. Here is a sample code.
using System;
using System.Text.RegularExpressions;
using System.Reflection;

namespace CompiledRegEx
{
   class Program
   {
      static void Main(string[] args)
      {
         if ( args.Length < 5 )
         {
            Console.WriteLine("Error: Insufficient Parameters. \nUsage: rexc ");
            return;
         }

         string pattern = args[0];
         string options = args[1];
         string className = args[2];
         string namespaceName = args[3];
         string assemblyName = args[4];

         RegexCompilationInfo ri = new RegexCompilationInfo(pattern,
            (RegexOptions)Convert.ToInt32(options),
            className,
            namespaceName,
            true);

         Regex.CompileToAssembly(new RegexCompilationInfo[] { ri },
            new AssemblyName(assemblyName));

         Console.WriteLine("Regular Expression successfully compiled to an assembly");
      }
   }
}
You can now use the new type from the compiled assembly just as you would use any other type from an assembly. The Match method of the new RegEx type does not take the pattern as a parameter. If you use a lot of static regular expressions in your code, this can provide significant gain in performance.

No comments:

Post a Comment