Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Faster Parsing

Posted on 2002-07-20
5
Medium Priority
?
264 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__
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

688 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