We have a Mid replacement. Any thoughts on FindString ?

Just starting out? Need help? Post your questions and find answers here.
yrret
User
User
Posts: 26
Joined: Wed May 26, 2004 9:05 pm

We have a Mid replacement. Any thoughts on FindString ?

Post by yrret »

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.
User avatar
jqn
User
User
Posts: 97
Joined: Fri Oct 31, 2003 3:04 pm

Post by jqn »

And StringField(String$, Index, Delimiter$) ?
And ReplaceString(String$, StringToFind$, StringToReplace$ [, Mode [, StartPosition]]) ?

Both commands are also used alot.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Post by wilbert »

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.

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))
KarLKoX
Enthusiast
Enthusiast
Posts: 681
Joined: Mon Oct 06, 2003 7:13 pm
Location: France
Contact:

Post by KarLKoX »

Fixed code :

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) )
Bench result :

PB ~ 1906
MSCVRT ~ 1140
"Qui baise trop bouffe un poil." P. Desproges

http://karlkox.blogspot.com/
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Still crashing here..
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Post by wilbert »

It doens't crash on my computer so it's hard for me to fix.

Maybe it'll help to use prototypes. I haven't used those so far since I don't know exactly how to use them.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Using prototypes is a tiny bit faster than Call(C)FunctionFast.
quidquid Latine dictum sit altum videtur
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

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.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Post by wilbert »

Thanks Traumatic !
That helps :D

The prototype way is not only faster but also the code looks cleaner.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

@Traumatic,

That works here, no crashing. Debugger off is showing MSVCRT around half the proctime of native PB with proto, 60% without. Thanks for posting.
yrret
User
User
Posts: 26
Joined: Wed May 26, 2004 9:05 pm

Post by yrret »

Thank you very much Traumatic !

That will really help others speed up programs
that need to use that code alot like I do. :D
SoulReaper
Enthusiast
Enthusiast
Posts: 372
Joined: Sun Apr 03, 2005 2:14 am
Location: England

Post by SoulReaper »

Wow thats Great :)

on xp1800 cpu

Pure Basic : 3515 <--- Winner :)
MSVCRT : 11375
MSVCRT Proto : 11657

@Traumatic
Thankyou for this Excellent Work...
I wonder where Microsoft went wrong :lol:

I love the speed of Pure Basic :) :twisted:
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Post by traumatic »

Hmm... wonder why everyone thanks ME since I only showed how to use prototypes...

Well, you're welcome anyway ;)
Good programmers don't comment their code. It was hard to write, should be hard to read.
Mikro
New User
New User
Posts: 7
Joined: Sun Sep 17, 2006 12:53 pm
Location: Ireland

What is FMid2?

Post by Mikro »

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.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: What is FMid2?

Post by traumatic »

Good programmers don't comment their code. It was hard to write, should be hard to read.
Post Reply