开发者

vb.net Regex Improving Performance with compiling and shared variables

I am writing some code that uses fixed regexs to search strings and pattern match.

Its simple stuff, but I want to improve regex performance with compiling (its a high traffic website).

I was thinking of compiling the regex and putting it in a Shared (static) variable inside a class.

Something like this:

Namespace Regexs

    Public Class UrlNickname

        Private Shared rgx As Regex = New Regex("^\/\w{4,20}$", RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant Or RegexOptions.Compiled)

        ''' <summary>
        ''' Returns a Nickname string if pattern found in Url, otherwise returns Empty string.
        ''' </summary>
        ''' <param name="url">The Url string to search.</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function ContainsNickname(url As String) As String
            If rgx.IsMatch(url) Then
                Return url.Substring(1, url.Length - 1)
            End If
            Return String.Empty
        End Function

    End Class

End Na开发者_Go百科mespace

Then you could use the function like this:

Dim url As String = HttpContext.Current.Request.RawUrl

Dim nickname As String = Regexs.UrlNickname.ContainsNickname(url)
If Not String.IsNullOrEmpty(nickname) Then
    //nickname pattern match found:
    //do something like RedirectToRoutePermanent
End If

Basically , I store the regex in a Shared (static) variable so that is only compiled once.

The function would then be called to check if a username pattern match was found on a 404 error page.

Whould this be the best approach for improving regex performance?

Note: I am not interested in a solution for 404 error page problems above, its just a simple example.


Further advances could then use a Shared generic list of regexs , like so:

Private Shared _rgxList As List(Of Regex)

Public Sub New()
    //get list of regex expressions from database and populate:
    _rgxList.Add(New Regex("blah", RegexOptions.Compiled))
    _rgxList.Add(New Regex("blah2", RegexOptions.Compiled))

Public Shared Function IsMatch(str) as Boolean
    With each reg in _rgxList
        return reg.IsMatch(str)

New() would be called on Application.Start event.


Looks fine. In addition, I'd make the Shared variable ReadOnly, to avoid accidental changes.

Note, however, that compiling regular expressions increases performance by about 30% at runtime, but it also has its downsides, in particular, it requires more memory. A nice comparison can be found here:

  • Coding Horror: To Compile or Not To Compile

So, there's no general "A is better than B" answer here. It all depends on your exact requirements. You might have to do measurements to find out what performs better in your particular case.

General advice on performance tuning: Make sure that something is really the performance bottleneck before improving it. It doesn't matter if your regex takes 0.01 or 0.02 seconds, if, for example, looking up the nickname in the databsae takes 2 seconds. Use built-in tools from the .net Framework (e.g. Stopwatch Class) or external tools (e.g. EQATEC Profiler) to find out where your bottlenecks are.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜