1. Computing & Technology

Discuss in my forum

Compiling Regular Expressions

Getting more speed from your compiled RegEx

By , About.com Guide

After examining the code for a while, I wondered if VB.NET was optomizing the RegEx by reusing a version 'compiled' in realtime. To test this guess, I coded a new version that forced the RegEx to be declared for every iteration of the test loop by coding the loop before the call rather than after.

Imports System.Diagnostics
Imports System.Text.RegularExpressions

Public Class CompiledRegEx
   Dim InlineTimeSpan As Integer
   Dim CompiledTimeSpan As Integer
   Dim LoopLimit As Integer
   Dim PhoneNum(9999) As String
   Dim I As Integer

   Private Sub TimeEm_Click( ...
      Dim myStopwatch As Stopwatch = New Stopwatch
      Dim myTimeSpan As New TimeSpan
      myStopwatch.Reset()
      myStopwatch.Start()
      If UseInline.Checked Then
         For I = 0 To LoopLimit
            UseInlineTest(I)
         Next
      End If
      myStopwatch.Stop()
      myTimeSpan = myStopwatch.Elapsed
      InlineTimeSpan = myTimeSpan.Milliseconds
      UsingInline.Text = myTimeSpan.ToString
      myStopwatch.Reset()
      myStopwatch.Start()
      If UseCompiled.Checked Then
         For I = 0 To LoopLimit
            UseCompiledTest(I)
         Next
      End If
      myStopwatch.Stop()
      myTimeSpan = myStopwatch.Elapsed
      CompiledTimeSpan = myTimeSpan.Milliseconds
      UsingCompiled.Text = myTimeSpan.ToString
      CalcImprovement()
   End Sub

   Private Sub UseInlineTest(ByVal I As Integer)
      Dim myRegexString As New Regex( _
         "^1?\s*-?\s*(\d{3}|\(\s*\d{3}\s*\))" & _
         "\s*-?\s*\d{3}\s*-?\s*\d{4}$")
      Dim RegExResult As Boolean
      RegExResult = _
         myRegexString.IsMatch(PhoneNum(I).ToString)
   End Sub

   Private Sub UseCompiledTest(ByVal I As Integer)
      Dim myCompiledRegex As New myRegExNS.myRegExType
      Dim RegExResult As Boolean
      RegExResult = _
         myCompiledRegex.IsMatch(PhoneNum(I).ToString)
   End Sub

   Private Sub CalcImprovement()
      Try
         Improvement.Text = _
            CStr((InlineTimeSpan / CompiledTimeSpan))
      Catch ex As Exception
         Improvement.Text = ""
      End Try
   End Sub

   Private Sub CompiledRegEx_Load( ...
      Dim PhoneNumSuf As Integer = 0
      Dim PhoneNumPre As String = "123-555-"
      LoopLimit = CInt(NumberOfIterations.Text) - 1
      For I = 0 To LoopLimit
         PhoneNum(I) = PhoneNumPre & I.ToString.PadLeft(4, "0"c)
      Next
   End Sub
End Class

This time, the results were much closer to what I expected: about 7 1/2 times faster!

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The lesson here is that if you can avoid re-executing the statement that declares your RegEx, it will add more to your execution speed than even compiling the RegEx.

©2012 About.com. All rights reserved.

A part of The New York Times Company.