We have a Mid replacement. Any thoughts on FindString ?
We have a Mid replacement. Any thoughts on FindString ?
I was really impressed with the speed gain with the FMid2 (Mid replacement) program. I found it consistently 10 time faster
then when using Mid. I was wondering with the expertise out there, if any one ever thought about, or think it's possible to improve the
FindString(String$, StringToFind$, StartPosition) speed?
That is one command I use alot, and it does slow the running program down when you use it alot.
then when using Mid. I was wondering with the expertise out there, if any one ever thought about, or think it's possible to improve the
FindString(String$, StringToFind$, StartPosition) speed?
That is one command I use alot, and it does slow the running program down when you use it alot.
The fastest way would be to use asm with sse functions.
Using C functions already makes a significant difference.
Turn off debugging for a fair comparisson.
Using C functions already makes a significant difference.
Turn off debugging for a fair comparisson.
Code: Select all
Global hMSVCRT.l = GetModuleHandle_("msvcrt.dll")
Global fStrStr.l = GetProcAddress_(hMSVCRT, "strstr")
Global fStrLen.l = GetProcAddress_(hMSVCRT, "strlen")
Procedure.l FindString2_(*String.l, *StringToFind.l, StartPosition.l)
If StartPosition <= CallCFunctionFast(fStrLen, *String)
If StartPosition < 2
StartPosition = 0
Else
StartPosition - 1
EndIf
Protected StrPos.l = CallCFunctionFast(fStrStr, *String + StartPosition, *StringToFind)
If StrPos
StrPos + 1 - *String
EndIf
ProcedureReturn StrPos
Else
ProcedureReturn 0
EndIf
EndProcedure
Macro FindString2(String, StringToFind, StartPosition)
FindString2_(@String, @StringToFind, StartPosition)
EndMacro
#Tries = 10000000
time = GetTickCount_()
For I = 0 To #Tries
p.l = FindString("The quick brown fox jumped over the lazy dog","lazy",7)
Next
MessageRequester("", Str(GetTickCount_()-time))
time = GetTickCount_()
For I = 0 To #Tries
p.l = FindString2("The quick brown fox jumped over the lazy dog","lazy",7)
Next
MessageRequester("", Str(GetTickCount_()-time))
Fixed code :
Bench result :
PB ~ 1906
MSCVRT ~ 1140
Code: Select all
Global hMSVCRT.l = LoadLibrary_("msvcrt.dll")
Global fStrStr.l = GetProcAddress_(hMSVCRT, "strstr")
Global fStrLen.l = GetProcAddress_(hMSVCRT, "strlen")
Procedure.l FindString2_(*String.l, *StringToFind.l, StartPosition.l)
If StartPosition <= CallCFunctionFast(fStrLen, *String)
If StartPosition < 2
StartPosition = 0
Else
StartPosition - 1
EndIf
Protected StrPos.l = CallCFunctionFast(fStrStr, *String + StartPosition, *StringToFind)
If StrPos
StrPos + 1 - *String
EndIf
ProcedureReturn StrPos
Else
ProcedureReturn 0
EndIf
EndProcedure
Macro FindString2(String, StringToFind, StartPosition)
FindString2_(@String, @StringToFind, StartPosition)
EndMacro
#Tries = 10000000
time = GetTickCount_()
For I = 0 To #Tries
p.l = FindString("The quick brown fox jumped over the lazy dog","lazy",7)
Next
first = GetTickCount_()-time
time = GetTickCount_()
For I = 0 To #Tries
p.l = FindString2("The quick brown fox jumped over the lazy dog","lazy",7)
Next
second = GetTickCount_()-time
MessageRequester("", "PureBasic : " + Str(first) + Chr(13) + "MSVCRT : " + Str(second) )
PB ~ 1906
MSCVRT ~ 1140
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
just for the sake of completeness...
Code: Select all
Global hMSVCRT.l = LoadLibrary_("msvcrt.dll")
Global fStrStr.l = GetProcAddress_(hMSVCRT, "strstr")
Global fStrLen.l = GetProcAddress_(hMSVCRT, "strlen")
PrototypeC strstr(a,b) : PrototypeC strlen(a)
Global strstr.strstr = GetProcAddress_(hMSVCRT, "strstr")
Global strlen.strlen = GetProcAddress_(hMSVCRT, "strlen")
Procedure.l FindString2_(*String.l, *StringToFind.l, StartPosition.l)
If StartPosition <= CallCFunctionFast(fStrLen, *String)
If StartPosition < 2
StartPosition = 0
Else
StartPosition - 1
EndIf
Protected StrPos.l = CallCFunctionFast(fStrStr, *String + StartPosition, *StringToFind)
If StrPos
StrPos + 1 - *String
EndIf
ProcedureReturn StrPos
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure.l FindString3_(*String.l, *StringToFind.l, StartPosition.l)
If StartPosition <= Strlen(*String)
If StartPosition < 2
StartPosition = 0
Else
StartPosition - 1
EndIf
Protected StrPos.l = Strstr(*String + StartPosition, *StringToFind)
If StrPos
StrPos + 1 - *String
EndIf
ProcedureReturn StrPos
Else
ProcedureReturn 0
EndIf
EndProcedure
Macro FindString2(String, StringToFind, StartPosition)
FindString2_(@String, @StringToFind, StartPosition)
EndMacro
Macro FindString3(String, StringToFind, StartPosition)
FindString3_(@String, @StringToFind, StartPosition)
EndMacro
#Tries = 10000000
time = GetTickCount_()
For I = 0 To #Tries
p.l = FindString("The quick brown fox jumped over the lazy dog","lazy",7)
Next
first = GetTickCount_()-time
time = GetTickCount_()
For I = 0 To #Tries
p.l = FindString2("The quick brown fox jumped over the lazy dog","lazy",7)
Next
second = GetTickCount_()-time
time = GetTickCount_()
For I = 0 To #Tries
p.l = FindString3("The quick brown fox jumped over the lazy dog","lazy",7)
Next
third = GetTickCount_()-time
MessageRequester("", "PureBasic : " + Str(first) + Chr(13) + "MSVCRT : " + Str(second) + Chr(13) + "MSVCRT / Proto : " + Str(third) )
Good programmers don't comment their code. It was hard to write, should be hard to read.
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
-
- Enthusiast
- Posts: 372
- Joined: Sun Apr 03, 2005 2:14 am
- Location: England
What is FMid2?
Sorry in advance if this question is really, really dumb.
What is FMid2 and where is it?
I just don't understand when the title mentions that we have a MID replacement? Where do you get this replacement?
Thanks a lot.
M.
What is FMid2 and where is it?
I just don't understand when the title mentions that we have a MID replacement? Where do you get this replacement?
Thanks a lot.
M.
Re: What is FMid2?
Good programmers don't comment their code. It was hard to write, should be hard to read.