Page 1 of 1

FindString and ReplaceString slower with #PB_String_NoCase

Posted: Mon Aug 29, 2022 7:46 am
by BarryG
When I run the below code, using the #PB_String_NoCase flag for the FindString() and ReplaceString() commands makes the loop run much slower than without. Surely NOT checking for the case should be faster, because the commands should just test the string as-is?

Reason for asking: I thought if the string had no text, then I should use #PB_String_NoCase to speed it up, but it actually makes it slower.

Code: Select all

Procedure.s RemoveDoubleSpaces(text$)
  While FindString(text$,"  ");,1,#PB_String_NoCase)
    text$=ReplaceString(text$,"  "," ");,#PB_String_NoCase)
  Wend
  ProcedureReturn text$
EndProcedure

DisableDebugger

start.q=ElapsedMilliseconds()
For a=1 To 500000
  text$=RemoveDoubleSpaces("Hello               World, How         Are    You                                 Today?")
Next
time.q=ElapsedMilliseconds()-start

EnableDebugger

Debug time ; 2800 ms without #PB_String_NoCase, but 5700 ms with it?

Re: FindString and ReplaceString slower with #PB_String_NoCase

Posted: Mon Aug 29, 2022 9:29 am
by Tawbie
Actually, I would have thought that with #PB_String_NoCase, PB would have to do more processing since now it has to test for a match for either upper or lower case versions of each character in the string, hence this would be slower.

Re: FindString and ReplaceString slower with #PB_String_NoCase

Posted: Mon Aug 29, 2022 9:40 am
by firace
Yes case-sensitive is always faster as it's just a bytewise comparison

Re: FindString and ReplaceString slower with #PB_String_NoCase

Posted: Mon Aug 29, 2022 10:35 am
by BarryG
Okay, weird. Thanks for the replies.

Re: FindString and ReplaceString slower with #PB_String_NoCase

Posted: Mon Aug 29, 2022 10:57 am
by NicTheQuick
Nothing weird here.

With '#PB_String_NoCase' you always have to check twice for every character. If you want to replace "a" with "z" then you have to look for "a" and "A" in the string. In practice "String$" and "StringToFind$" are converted to lower case I guess and then the search takes place with these lower case versions of the strings but replacing takes place in the original string. Of course this is more overhead than simply searching and replacing the characters as-is.

Re: FindString and ReplaceString slower with #PB_String_NoCase

Posted: Mon Aug 29, 2022 2:12 pm
by jacdelad
Can't check right now, but maybe

Code: Select all

FindString(LCase(String1$), LCase(String2$))
is faster than

Code: Select all

 FindString(String1$,String2$,#PB_String_NoCase)

Re: FindString and ReplaceString slower with #PB_String_NoCase

Posted: Mon Aug 29, 2022 3:21 pm
by Little John
jacdelad wrote: Mon Aug 29, 2022 2:12 pm

Code: Select all

 FindString(String1$,String2$,#PB_String_NoCase)
Or maybe someone would prefer to use

Code: Select all

 FindString(String1$, String2$, 1, #PB_String_NoCase)
instead? :twisted:

Re: FindString and ReplaceString slower with #PB_String_NoCase

Posted: Mon Aug 29, 2022 3:27 pm
by jacdelad
Yeah, you're right. Like I said, can't test it right now...and made a rookie mistake. Thanks for correcting.