Monday, 19 August 2013

Speed of VB.net Regex?

Speed of VB.net Regex?

Is the regex code in VB.net known to be slow?
I took over some code that was cleaning large amounts of text data. The
code ran fairly slow, so I was looking for some ways to speed it up. I
found a couple functions that got run a lot that I thought might be part
of the problem.
Here's the original code for cleaning a phone number:
Dim strArray() As Char = strPhoneNum.ToCharArray
Dim strNewPhone As String = ""
Dim i As Integer
For i = 0 To strArray.Length - 1
If strArray.Length = 11 And strArray(0) = "1" And i = 0 Then
Continue For
End If
If IsNumeric(strArray(i)) Then
strNewPhone = strNewPhone & strArray(i)
End If
Next
If Len(strNewPhone) = 7 Or Len(strNewPhone) = 10 Then
Return strNewPhone
End If
I rewrote the code to eliminate the array and looping using regex.
Dim strNewPhone As String = ""
strNewPhone = Regex.Replace(strPhoneNum, "\D", "")
If strNewPhone = "" OrElse strNewPhone.Substring(0, 1) <> "1" Then
Return strNewPhone
Else
strNewPhone = Mid(strNewPhone, 2)
End If
If Len(strNewPhone) = 7 Or Len(strNewPhone) = 10 Then
Return strNewPhone
End If
After running a couple tests, the new code is significantly slower than
the old. Is regex in VB.net slow, did I add some other thing that is the
issue, or is the original code just fine the way it was?

No comments:

Post a Comment