Re: Instr()
- From: "Mike Williams" <mike@xxxxxxxxxxxxxxxxx>
- Date: Thu, 2 Aug 2007 22:51:46 +0100
"Jack" <replyto@it> wrote in message news:%23Pbo0dU1HHA.1208@xxxxxxxxxxxxxxxxxxxxxxx
I am suprised to find how fast Instr() API is! It can
find string in 500 KB of data with lightning speed.
I wonder how does Instr() work?
Instr() API? There is no Instr() API! Presumably you mean the VB6 Instr function? It is fast, but that's mainly because computers are fast these days.
The VB Instr function is fast mainly because (like most other functions) it simply calls some "under the hood" routine that has been written either in machione code or (more likely) in ASM and it deals with the data as simple numbers, rather than as strings and substrings, which are fairly slow in standard VB code because of the fact that VB stores all string data using two bytes per character and so various Unicode conversions need to be carried out for most jobs.
As far as I know, the VB Instr function simply looks through the entire string data "one character at a time" until it finds the first character of the search string, and then checks to see if the following characters also match. If not then it carries on until it either finds or match or reaches the end of the string. This means that it needs to look at every character in the string in order to be sure of finding what it is looking for. Nevertheless, even that approach is fast these days because the machines themselves are blindingly fast.
A faster algorithm (which I'm fairly sure the VB Instr function does not use) is the Boyer-Moore algorithm (although there are others). These algorithmns are very clever and if the VB Instr function used them it would be even faster still. Check out:
http://en.wikipedia.org/wiki/Boyer-Moore_string_search_algorithm
I reckon that using that algorithm it would be possible to write standard VB "check the data bytes" code that would require a lot of code lines to program but that would nevertheless beat the Instr function for speed when run as a native code compiled exe.
Instr is still quite fast though. I would agree with you there.
Mike
.
- Follow-Ups:
- Re: Instr()
- From: Michael C
- Re: Instr()
- References:
- Instr()
- From: Jack
- Instr()
- Prev by Date: Re: Picture UnPerfect
- Next by Date: Re: listview checked items
- Previous by thread: Re: Instr()
- Next by thread: Re: Instr()
- Index(es):
Relevant Pages
|