FindString and ReplaceString slower with #PB_String_NoCase

Just starting out? Need help? Post your questions and find answers here.
BarryG
Addict
Addict
Posts: 4318
Joined: Thu Apr 18, 2019 8:17 am

FindString and ReplaceString slower with #PB_String_NoCase

Post 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?
Tawbie
User
User
Posts: 35
Joined: Fri Jul 10, 2020 2:36 am
Location: Australia

Re: FindString and ReplaceString slower with #PB_String_NoCase

Post 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.
firace
Addict
Addict
Posts: 947
Joined: Wed Nov 09, 2011 8:58 am

Re: FindString and ReplaceString slower with #PB_String_NoCase

Post by firace »

Yes case-sensitive is always faster as it's just a bytewise comparison
BarryG
Addict
Addict
Posts: 4318
Joined: Thu Apr 18, 2019 8:17 am

Re: FindString and ReplaceString slower with #PB_String_NoCase

Post by BarryG »

Okay, weird. Thanks for the replies.
User avatar
NicTheQuick
Addict
Addict
Posts: 1565
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: FindString and ReplaceString slower with #PB_String_NoCase

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
jacdelad
Addict
Addict
Posts: 2074
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: FindString and ReplaceString slower with #PB_String_NoCase

Post 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)
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/2*DX517, 164TB+82TB+28TB+2TB SSD
Little John
Addict
Addict
Posts: 4843
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: FindString and ReplaceString slower with #PB_String_NoCase

Post 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:
User avatar
jacdelad
Addict
Addict
Posts: 2074
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: FindString and ReplaceString slower with #PB_String_NoCase

Post by jacdelad »

Yeah, you're right. Like I said, can't test it right now...and made a rookie mistake. Thanks for correcting.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/2*DX517, 164TB+82TB+28TB+2TB SSD
Post Reply