We have a Mid replacement. Any thoughts on FindString ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

this can be also written using Import/EndImport with is the shortest way i think.

Code: Select all

ImportC "msvcrt.lib"
  strstr(*lpFirst, *lpSrch) ; Finds the first occurrence of a substring within a string. The comparison is case sensitive.
EndImport

Macro FindString2(String, StringToFind, StartPosition) 
  (strstr(@String + StartPosition - 1, @StringToFind) + 1 - @String)
EndMacro

#Tries = 10000000

String$ = "The quick brown fox jumped over the lazy dog"

t0 = ElapsedMilliseconds()
For i = 0 To #Tries : p = FindString(String$, "lazy", 7) : Next
t1 = ElapsedMilliseconds() - t0

t0 = ElapsedMilliseconds()
For i = 0 To #Tries : p = FindString2(String$, "lazy", 7) : Next
t2 = ElapsedMilliseconds() - t0

MessageRequester("Results", "FindString() : " + Str(t1) + #LF$ + "StrStr() : " + Str(t2))


Results on my AthlonXP 2800+ (2.08Ghz) :

FindString() : 2062ms
StrStr() : 1360ms
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Post Reply