Faster Parsing

OK.  I parse HTML.

A lot of it.
I need to make this procedure faster--

I think that i need to track which letters are the most popular and then make a complex select statement sorted in the order of most popular->least popular.

But thats just a detail; its not the meat and bones.

i wanna keep this statement simple and quick.

i think that most of my performance hit is on the repeated ASC()-- i think that maybe i should build a collection of ASCII symbols..  (but to be honest i dont know how to do a collection (but i can do an array))

and I would like to
a). optimize my code for speed
b). find a replacement for the Asc(function)

My Code
Option Explicit

private newString

Public Sub CleanString(strIncoming As String)
On Error GoTo errhandler

newString = ""

'Determine length of string
Dim i As Integer
Dim strLen As Integer
Dim char As String
Dim ascChar As Integer

strLen = Len(strIncoming)
For i = 1 To strLen

char = Mid(strIncoming, i, 1)
ascChar = Asc(char)

If ascChar >= 46 And ascChar <= 57 Then
    newString = newString & char
  GoTo nextI
End If

If ascChar >= 63 And ascChar <= 90 Then
    newString = newString & char
  GoTo nextI
End If

If ascChar >= 97 And ascChar <= 122 Then
    newString = newString & char
  GoTo nextI
End If

newString = newString + " "
Next i

Exit Sub

MsgBox Err.Number & " - " & Err.Description, vbOKOnly
Resume Next
Resume cleanexit
End Sub

other information-

i am making this a class and compiling into a DLL(so that i can move to COM+ down the road (or or whatever its called))
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

PaulHewsConnect With a Mentor Commented:
My tests show this to be almost 20 times faster.

Public Function fCleanString2(strIncoming As String) As String
    Dim sngTime As Single
    Dim bytIn() As Byte
    Dim bytOut() As Byte
    Dim i As Long
    sngTime = Timer
'    On Error GoTo errhandler
    bytIn = StrConv(strIncoming, vbFromUnicode)
    ReDim bytOut(LBound(bytIn) To UBound(bytIn))

    For i = 0 To UBound(bytIn)
        Select Case bytIn(i)
        Case 46 To 57, 63 To 90, 97 To 122
            bytOut(i) = bytIn(i)
        Case Else
            bytOut(i) = 32 'space
        End Select
    Next i
    fCleanString2 = StrConv(bytOut, vbUnicode)
    Debug.Print "fCleanString2 " & Timer - sngTime
    Exit Function
    MsgBox Err.Number & " - " & Err.Description, vbOKOnly
    Resume Next
End Function
__Holly__Author Commented:
oh and what i do in the next step is to split this into an array based on a space.

i should move that split function back into this--

but its a mute point until i can get this running faster...
__Holly__Author Commented:
thanks paul ill test that out.
This was my best, but I think it's still about 4 times slower than Paul's

Public Function fCleanString3(strIncoming As String) As String
Dim vStart
Dim vEnd
Dim i
Dim strTemp

    vStart = GetTickCount
    strTemp = strIncoming
   For i = 0 To 255
        If (i < 46) Or (i > 57 And i < 63) Or (i > 90 And i < 97) Or (i > 122) Then
            strTemp = Replace(strTemp, Chr(i), " ")
        End If
   Next i
   fCleanString3 = strTemp
   vEnd = GetTickCount
    Label1.Caption = "Elapsed Milli-seconds: " & (vEnd - vStart)

   Exit Function
   MsgBox Err.Number & " - " & Err.Description, vbOKOnly
   Resume Next
End Function
__Holly__Author Commented:
i didnt have the chance to run performance tests/determine a ratio--

but it sure seems a lot faster.

All Courses

From novice to tech pro — start learning today.