HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

VB6: String Searching Performance

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
vb6stringsearchingperformance

Problem

5 years ago, when i was in love with VB, I created this simple program to detect if a file is virus or not.

What I'd like is to see if you can help me optimize it, speed-wise.

I open an executable file in a rich textbox control and use the Instr method to search for occurrences of predefined string. The problem is that the Instr method is damn slow when searching files as small as 512KB.

Can the speed be enhanced?

If InStr(1, rt.Text, "scripting.filesystemobject") Then
 It does something with FSO
End If

If InStr(1, rt.Text, "thisprogramcannotberunindosmode") Or InStr(1, rt.Text, "thisprogrammustberununderwin32") Then
  Probably a DOS program
 Else
  looks like a GUI program
End If

If InStr(1, rt.Text, "hkey_local_machine") Then
 Does something with registry
End If
If InStr(1, rt.Text, "hkey_users") Then
 Does something with registry
End If
If InStr(1, rt.Text, "hkey_current_user") Then
 Does something with registry
End If
If InStr(1, rt.Text, "hkey_classes_root") Then
 Does something with registry
End If
If InStr(1, rt.Text, "hkey_current_config") Then
 Does something with registry
End If

Solution

In VB2010 the instr out performed indexof

Dim ipsum As String = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

    Dim tries As Integer = 100000
    Dim stpw As New Stopwatch
    Dim iof As Integer
    Dim f As String = "sint"

    stpw.Reset()
    stpw.Start()

    For x As Integer = 1 To tries
        iof = ipsum.IndexOf(f)
    Next
    stpw.Stop()
    Label1.Text = stpw.ElapsedMilliseconds.ToString("n2")

    stpw.Reset()
    stpw.Start()

    For x As Integer = 1 To tries
        iof = InStr(ipsum, f)
    Next
    stpw.Stop()
    Label2.Text = stpw.ElapsedMilliseconds.ToString("n2")

Code Snippets

Dim ipsum As String = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."

    Dim tries As Integer = 100000
    Dim stpw As New Stopwatch
    Dim iof As Integer
    Dim f As String = "sint"

    stpw.Reset()
    stpw.Start()

    For x As Integer = 1 To tries
        iof = ipsum.IndexOf(f)
    Next
    stpw.Stop()
    Label1.Text = stpw.ElapsedMilliseconds.ToString("n2")

    stpw.Reset()
    stpw.Start()

    For x As Integer = 1 To tries
        iof = InStr(ipsum, f)
    Next
    stpw.Stop()
    Label2.Text = stpw.ElapsedMilliseconds.ToString("n2")

Context

StackExchange Code Review Q#2059, answer score: 5

Revisions (0)

No revisions yet.