Re: Instr()



"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




.



Relevant Pages

  • Re: detecting characters on RS232-Interface
    ... Append new data to a Private or Static string variable. ... Use the InStr ... function to scan the data for a "U" character. ... other pattern evaluation functions, such as Mid). ...
    (microsoft.public.vb.general.discussion)
  • Re: How do I create a program to search a list of words within a d
    ... You can then use the Instr function to check whether each ... If you need the search to be case-insensitive, convert the string to lowercase after extracting it, and make sure you have loawercase versions of your list of words before using Instr. ... >I want a program to search and identify a word/words/characters that appears within a WORD document. ...
    (microsoft.public.word.vba.general)
  • RE: How can I create a outlook appointment item from data in a e-m
    ... If you know the starting point of where the text you want occurs, use InStr ... Eric Legault (Outlook MVP, MCDBA, MCTS: ... if I have a string that looks like this: ... Once you get the text you need, it's easy to create a new appointment item: ...
    (microsoft.public.outlook.program_vba)
  • Parsing a string
    ... The function also has two optional input parameters, ... default delimiter value in the function definition line ... then using the instr() functions. ... Dim ArrayBreakPosAs Integer, strPrefix As String ...
    (microsoft.public.access.queries)
  • Re: replacing spaces?
    ... either the replace method to handle the entire worksheet or a range in the ... > Public Function RemoveSpaces(strInput As String) ... At the core of the function is the InStr() ... and the character to be located. ...
    (microsoft.public.excel.programming)