Results 1 to 7 of 7

Thread: [RESOLVED] How to improve "search" performance?

  1. #1

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Resolved [RESOLVED] How to improve "search" performance?

    Hello,

    Im using For loop to cycle trough multiline texbox with lots of text, and Mid$ function to search for keywords within that text.

    I have compared the search from Windows notepad and search from my application and my is several times slower when searching the same text?

    Are there any ways to improve my search algorithm or a different algorithm.

    I have a subquestion too. How can i copy the selected text from textbox using CTRL + C. Not the whole texbox, only text that is selected.

    Regards!

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to improve "search" performance?

    1. Not sure why you would need a For loop to search a textbox. InStr() can find selected text pretty quickly. You might want to post key parts of your search routine.

    2. To copy selected text to the clipboard.
    ClipBoard.Clear
    Clipboard.SetText Text1.SelText
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to improve "search" performance?

    Thanks LaVolpe

    Here is my search routine:

    Code:
    If KeyAscii = vbKeyReturn Then
            Dim a As String
            Dim br, b As Integer
            a = LCase$(Text2.Text)
            b = Len(Text2.Text)
            For br = Pocetak To Len(Text1.Text)
                If LCase$(Mid$(Text1.Text, br, b)) = a Then
                    Text1.SetFocus
                    Text1.SelStart = br - 1
                    Text1.SelLength = b
                    KeyAscii = 0
                    If Pocetak < Len(Text1.Text) Then
                        Pocetak = br + 1
                    Else
                        Pocetak = 1
                    End If
                    Exit Sub
                End If
            Next br
            KeyAscii = 0
            Pocetak = 1
        End If
    Edit:

    Program needs to remember where search ended so next time i hit enter it starts from that spot.
    Pocetak is global variable.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to improve "search" performance?

    I think this would be much faster...
    Code:
    Dim lPos As Long
    If Pocetak < 1 Then Pocetak = 1
    lPos = InStr(Pocetak, Text1.Text, Text2.Text, vbTextCompare)
    ' change vbTextCompare to vbBinaryCompare for case-sensitivity
    If lPos = 0 Then 
        ' text2 not found in text1
       ' may want to reset Pocetak here?
    Else
        Text1.SelStart = lPos
        Text1.SelLength = Len(Text2.Text)
        Text1.SetFocus
        Pocetak = lPos + 1
    End If
    Does that work better?
    Note Pocetak cannot be zero, it must start at a minimum of 1.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to improve "search" performance?

    Yeah i know, my Pocetak is Globaly declared and is set to 1 on form load.

    Ok, so this InStr function compres text1 to text2, its not case sensitive and it starts from Pocetak of text1? So if Pocetak is 51, InStr would search from 51st character?

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How to improve "search" performance?

    Yes to all except....
    1. The case-sensitivity part. You dicatate that by changing the last parameter as I commented in the sample code.
    2. It compares Text2 to Text1. It checks if Text2 is in Text1. If I got them reversed, then reverse them in the sample code.

    Even though Pocetak is set to 1 on form load, there are other places you will want to reset it back to 1. For example, if no match is found? If user opts to search from beginning? If Text1 is replaced maybe? Maybe if the user clicks inside of Text1, then maybe Pocetak should be set to Text1.SelStart? Just think about it as if you were the user and wanted to search... when would you want to search from the beginning?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: How to improve "search" performance?

    Yeah, you are right about those resets of Pocetnik. I'll implement some of your advices. Thanks a lot for the code, it works perfectly fast. I didnt know about this InStr function, well i heard about it, and ive seen it before but it didnt fell on my mind now when i needed it.

    Resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width