Solved

Faster Parsing

Posted on 2002-07-20
5
231 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now