Results 1 to 18 of 18

Thread: [RESOLVED] Search engine speed

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Resolved [RESOLVED] Search engine speed

    Hello,

    Anybody know of the fastest search engine in VB?

    I loaded the whole kjv Bible into a rtf box and it took me 37 seconds to fine the word "day."

    Code:
    Private Function WordCount(strLine As String, strWord As String) As Long
       WordCount = (Len(strLine) - Len(Replace(strLine, strWord, ""))) / Len(strWord)
    
    End Function
    
    Private Sub cmdFindNext_Click()
     Dim intIdx As Long
     Dim CountLeft As Integer
     Dim response As String
     Dim strtext As String
     Dim NumLoop As Integer
    
     FoundLabel.Visible = True
    
     strtext = RTFText1.Text
    
     RTFText1.SetFocus
    
     If txtSearch.Text = "" Then
       COUNTED = 0
       Exit Sub
     End If     
     
     COUNTED = COUNTED + 1
     CountLeft = WordCount(strtext, txtSearch.Text) - COUNTED
    
     NumLoop = NumLoop + 1
     FoundLabel.Caption = " Hit #" & COUNTED & " of " & WordCount(strtext, txtSearch.Text) & ", there are "  & CountLeft & " Left"
    
     RTFText1.Find txtSearch, RTFText1.SelStart + 1, Len(RTFText1.TextRTF)
    
     If NumLoop = 2 Then
       FoundLabel.Caption = " Hit #" & COUNTED - 1 & " of " & WordCount(strtext, txtSearch.Text)
       Beep
       COUNTED = 0
       Exit Sub
     End If
        POS = Form1a.RTFText1.SelStart
        POS = InStr(POS + 1, Form1a.RTFText1.Text, txtSearch.Text)
             
      If POS = 0 Then
                 
       response = MsgBox("your search for ~ " & txtSearch & " ~ ... was found a total of " & COUNTED - 1 & " time(s). ...  Do you want to start from the beginning?", vbYesNo)
       COUNTED = 0
       FoundLabel.Caption = " Hit #" & COUNTED & " of " & WordCount(strtext, txtSearch.Text)
    
               If response = vbNo Then
                   Exit Sub
               Else
    
                   RTFText1.Find txtSearch, RTFText1.SelStart + 1, Len(RTFText1.TextRTF)
             
                   Form1a.RTFText1.SelStart = POS 'GOES TO THE TOP
               End If
      End If
    
    If OptVar1 = True Then
       Exit Sub
    End If
    
    RTFText1.SelColor = vbRed
    RTFText1.SetFocus
    SendKeys "{down 20}"
    
    If SearchBoldVar = True Then
       RTFText1.SelBold = Not RTFText1.SelBold
    End If
    
    If SearchItalicVar = True Then
       RTFText1.SelItalic = Not RTFText1.SelItalic
    End If
    
    If SearchUnderlineVar = True Then
       RTFText1.SelUnderline = Not RTFText1.SelUnderline
    End If
    
    If SearchRegularVar = True Then
       SearchItalicVar = False
       SearchBoldVar = False
       SearchUnderlineVar = False
    End If
    
    Exit Sub
     
    End Sub
    Last edited by GARY MICHAEL; Mar 8th, 2010 at 05:17 PM.
    Thanks,
    GARY

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Search engine speed

    You haven't shown your WordCount function, but it is being called twice (with exactly the same parameters) before you even begin searching. In some cases you also run it again (with exactly the same parameters again) after the search.

    Rather than re-running it multiple times, run it once and store the result.


    There are several other things about your code that are less than ideal, such as:
    • Using a String to store the result of a MsgBox... when the data type it returns is numeric (so use either Long, or vbMsgBoxResult).
    • Joining two hard-coded strings (" " & "Hit #"), when you could just put the result (" Hit #").
    • Terrible indenting, which makes it extremely hard to read - and thus check what it is doing.
    Due to the last point, I'm not even going to read most of your code.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Re: Search engine speed

    Thanks for your comments. Maybe somebody else will have the patience to go through it for me. I appreciate what you DID say. Will the things you mentioned slow my search down?
    Last edited by GARY MICHAEL; Mar 8th, 2010 at 01:49 PM.
    Thanks,
    GARY

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Re: Search engine speed

    You know the
    Code:
    " " & "Hit #",
    code was pretty stupid. I know better. Don't know what I was thinking about. Part of this code was done a long time ago and I never noticed the need to change it.
    Thanks,
    GARY

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Search engine speed

    Quote Originally Posted by GARY MICHAEL View Post
    Thanks for your comments. Maybe somebody else will have the patience to go through it for me.
    That will be significantly more likely if you stop intentionally making it hard to read.
    Will the things you mentioned slow my search down?
    Three of the four things I mentioned definitely will.

    We cannot tell for sure how much each would be by (especially as we can't see all of the code), but the first could be very significant.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Re: Search engine speed

    I don't know what would make you think that I would intentionally make it hard to read. That is a pretty strong inditement on my character. You are mistaken and you don't know me well enough to make such a statement. I need to learn how to code in such a fashion that will be more readable. I'll try to do better.
    Last edited by GARY MICHAEL; Mar 8th, 2010 at 04:29 PM.
    Thanks,
    GARY

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Search engine speed

    Your indenting is completely random, which makes it even harder to read than if there was no indenting at all.

    You did that indenting, which means that to some degree it was intentional - even if you didn't consciously set out to make it hard to read.

    If you were writing a letter, would you start each line a random amount across the page? I'm assuming not, but that is essentially what you have done.


    The only proper indenting you have got is on these three lines:
    Code:
      End If
    End If
    If OptVar1 = True Then
    ..but due to the rest of the code, we have no idea which If each of those End If's refers to - and the only way to find out is to read each of the previous lines of code to work out where it is.

    With proper indenting there would be no reading needed, and you could see almost instantly.


    As a simple guideline, each time you have a code structure like If-Else-End If (or For-Next / Do-Loop / etc), you should have the same number of spaces at the start of each of those lines, and more spaces in front of all of the lines between them, eg:
    Code:
      If response = vbNo Then
        Exit Sub
      Else
        RTFText1.Find txtSearch, RTFText1.SelStart + 1, Len(RTFText1.TextRTF)
        Form1a.RTFText1.SelStart = POS 'GOES TO THE TOP
      End If
    Now you can see at a glance which If the End If (and Else) refer to.

    When you have one code structure inside another, you increase the indenting as apt, eg:
    Code:
    If Number > 5 Then
      If Number > 10 Then
        MsgBox "Number is more than 10"
      Else
        MsgBox "Number is more than 5, but not more than 10"
      End If
    End If
    ..and again, you can tell at a glance which refers to which.


    Most people use the Tab key for indenting, and VB allows you to easily indent a whole group of lines at the same time, by selecting the lines and then pressing Tab. You can set the amount of spaces a Tab causes in VB's options (the default is 4, I personally prefer 2).

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Re: Search engine speed

    Oh! Now I get it. I think. I will change my post above and see how it goes. Thanks SI.
    Thanks,
    GARY

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Re: Search engine speed

    Alright Si, check me out. Is my post better as changed above?
    I've included this code too.

    Code:
    Private Function WordCount(strLine As String, strWord As String) As Long
       WordCount = (Len(strLine) - Len(Replace(strLine, strWord, ""))) / Len(strWord)
    
    End Function
    Last edited by GARY MICHAEL; Mar 8th, 2010 at 05:22 PM.
    Thanks,
    GARY

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Search engine speed

    That is much better.

    It isn't quite perfect, because there is still some randomness in there, mainly in this section:
    Code:
     End If
        POS = Form1a.RTFText1.SelStart
        POS = InStr(POS + 1, Form1a.RTFText1.Text, txtSearch.Text)
             
      If POS = 0 Then
                 
       response = MsgBox("your search for ~ " & txtSearch & " ~ ... was found a total of " & COUNTED - 1 & " time(s). ...  Do you want to start from the beginning?", vbYesNo)
       COUNTED = 0
       FoundLabel.Caption = " Hit #" & COUNTED & " of " & WordCount(strtext, txtSearch.Text)
    
               If response = vbNo Then
                   Exit Sub
               Else
    
                   RTFText1.Find txtSearch, RTFText1.SelStart + 1, Len(RTFText1.TextRTF)
             
                   Form1a.RTFText1.SelStart = POS 'GOES TO THE TOP
               End If
      End If
    ..which should be like this:
    Code:
     End If
     POS = Form1a.RTFText1.SelStart
     POS = InStr(POS + 1, Form1a.RTFText1.Text, txtSearch.Text)
             
     If POS = 0 Then
                 
       response = MsgBox("your search for ~ " & txtSearch & " ~ ... was found a total of " & COUNTED - 1 & " time(s). ...  Do you want to start from the beginning?", vbYesNo)
       COUNTED = 0
       FoundLabel.Caption = " Hit #" & COUNTED & " of " & WordCount(strtext, txtSearch.Text)
    
       If response = vbNo Then
         Exit Sub
       Else
    
         RTFText1.Find txtSearch, RTFText1.SelStart + 1, Len(RTFText1.TextRTF)
             
         Form1a.RTFText1.SelStart = POS 'GOES TO THE TOP
       End If
     End If
    Note that the amount of spaces you should use for indenting is a matter of opinion, just use whatever you feel happiest with (but always use the same amount, and set the VB option for spaces per Tab to match it).



    The next thing to do is act on the rest of post #2 (the first half, and the first bullet point).

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Re: Search engine speed

    Thanks again.

    What would you change in my code to speed it up to the max?
    Thanks,
    GARY

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Search engine speed

    The changes I've already recommended are likely to produce a noticeable improvement - it wouldn't be surprising if the 37 seconds is reduced to something less than 30 (perhaps even under 20).

    To make it faster than that would require replacing RTFText1.Find with something else, and/or tidying up the rest of the code (for example, NumLoop can only ever have the value 1, yet you have code which checks if it is 2).

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Re: Search engine speed

    If you could change my code to reflect what you are talking about, I could see it better and compare to two codes. Can you do that?
    Thanks,
    GARY

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Re: Search engine speed

    Here is a revision of my code.

    Code:
    Private Function WordCount(strLine As String, strWord As String) As Long
       WordCount = (Len(strLine) - Len(Replace(strLine, strWord, ""))) / Len(strWord)
    
    End Function
    
    
    Private Sub cmdFindNext_Click()
    
    Dim intIdx As Long
    Dim CountLeft As Integer
    Dim response As Long
    Dim strtext As String
    
    FoundLabel.Visible = True
            
    strtext = RTFText1.Text
    
    RTFText1.SetFocus
    
    If txtSearch.Text = "" Then
        COUNTED = 0
        Exit Sub
    End If
         
          
    COUNTED = COUNTED + 1
    CountLeft = WordCount(strtext, txtSearch.Text) - COUNTED
    
    FoundLabel.Caption = " Hit #" & COUNTED & " of " & WordCount(strtext, txtSearch.Text) & ", there are " & CountLeft & " Left"
    
    RTFText1.Find txtSearch, RTFText1.SelStart + 1, Len(RTFText1.TextRTF)
    
        POS = Form1a.RTFText1.SelStart
        POS = InStr(POS + 1, Form1a.RTFText1.Text, txtSearch.Text)
             
    If POS = 0 Then
                 
        response = MsgBox("your search for ... " & txtSearch & " ... was found a total of " & COUNTED - 1 & " time(s). ...  Do you want to start from the beginning?", vbYesNo)
        COUNTED = 0
        FoundLabel.Caption = " " & "Hit #" & COUNTED & " of " & WordCount(strtext, txtSearch.Text)
    
        If response = vbNo Then
            Exit Sub
        Else
    
            RTFText1.Find txtSearch, RTFText1.SelStart + 1, Len(RTFText1.TextRTF)
           
            Form1a.RTFText1.SelStart = POS 'GOES TO THE TOP
        End If
    End If
    
    
    RTFText1.SelColor = vbRed
    RTFText1.SetFocus
    SendKeys "{down 20}"
    
     
    End Sub
    Last edited by GARY MICHAEL; Mar 8th, 2010 at 07:37 PM.
    Thanks,
    GARY

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Re: Search engine speed

    I don't think VB has the speed I'm looking for. I have a program and has a very, very large file that searches the whole document in a split second. I think that it was written in visual c++. That is the kind of speed I need. I will hunt for a visual C++ search engine.

    How do I end this this thread, since it was not resolved? Do I unsubscribe to it?
    Last edited by GARY MICHAEL; Mar 9th, 2010 at 08:41 AM.
    Thanks,
    GARY

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Search engine speed

    VB has the speed, but you aren't attempting making use of it... instead you are using one of the easiest methods available, doing very little to improve it even after apt advice, and are pretending to yourself that somehow the speed is the fault of the language.

    Based on the level of coding skills and effort you have shown, there is no reason to believe that in a reasonable time frame you could get a C based program working at all (as it is very strict in terms of data types, etc), let alone get it to be faster than what you have above.

    If you want more speed you have to put in extra effort... yet it took 11 replies for you to get around to this:
    Here is a revision of my code.
    That has finally corrected one of the simplest issues I mentioned back in post #2, but it was the smallest of the three speed based issues (its main effect is reducing bugs).


    In terms of what I have advised you to do already, the most significant speed issue by far is to only call the WordCount function once, and store the result to a variable - then use the variable everywhere that you currently call the function.

    As you currently have it, each time you run it you are using exactly the same parameters, so it is doing exactly the same work again, and returning exactly the same result. This is not specifically a programming issue, it applies in many other situations too... why do the same work repeatedly, when re-doing it has no extra effect?
    If you could change my code to reflect what you are talking about, I could see it better and compare to two codes. Can you do that?
    No, you do it yourself - and show us what you come up with.

    If it doesn't work, we can help correct it, and explain why those corrections are needed. If it works but has room for improvement, we can tell you how to do that, and why. Whichever way it goes, you will learn far more than if we just gave you code.

    Doing things for you takes us more time, and leads to more of the same... and as we are giving up our own free time to help a random stranger, does that sound like a reasonable request to make?
    How do I end this this thread, since it was not resolved? Do I unsubscribe to it?
    If you want to give up then just mark it as Resolved so that people don't keep trying to help, and reply (and/or edit the title) to say that you have given up so that people who search can tell.

  17. #17
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: [RESOLVED] Search engine speed

    GARY MICHAEL,

    Take a look at this for indenting your code with just one click of a button:
    http://www.vbforums.com/showthread.php?t=479449

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2002
    Location
    Tennessee
    Posts
    378

    Re: [RESOLVED] Search engine speed

    Thanks CV Michael.
    Thanks,
    GARY

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