Solved

Faster Parsing

Posted on 2002-07-20
5
239 Views
Last Modified: 2010-05-02
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 + " "
nextI:
Next i

cleanexit:
Exit Sub

errhandler:
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 COM.net or whatever its called))
0
Comment
Question by:__Holly__
  • 3
5 Comments
 
LVL 1

Author Comment

by:__Holly__
ID: 7166979
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...
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 350 total points
ID: 7167129
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
   
errhandler:
    MsgBox Err.Number & " - " & Err.Description, vbOKOnly
    Resume Next
   
End Function
0
 
LVL 1

Author Comment

by:__Holly__
ID: 7168211
thanks paul ill test that out.
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7172333
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
   
errhandler:
   MsgBox Err.Number & " - " & Err.Description, vbOKOnly
   Resume Next
   
End Function
0
 
LVL 1

Author Comment

by:__Holly__
ID: 7173686
i didnt have the chance to run performance tests/determine a ratio--

but it sure seems a lot faster.

thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question