|
-
Jan 27th, 2009, 10:35 AM
#1
Thread Starter
Fanatic Member
[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!
-
Jan 27th, 2009, 10:52 AM
#2
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
-
Jan 27th, 2009, 11:13 AM
#3
Thread Starter
Fanatic Member
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.
-
Jan 27th, 2009, 11:22 AM
#4
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.
-
Jan 27th, 2009, 11:31 AM
#5
Thread Starter
Fanatic Member
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?
-
Jan 27th, 2009, 11:33 AM
#6
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?
-
Jan 27th, 2009, 11:45 AM
#7
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|