LCase faster than #PB_String_NoCase

Everything else that doesn't fall into one of the other PB categories.
User avatar
jacdelad
Enthusiast
Enthusiast
Posts: 139
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa
Contact:

LCase faster than #PB_String_NoCase

Post by jacdelad »

I just found out that when searching within a string and case doesn't matter you better use LCase() instead of the #PB_String_NoCase parameter. The difference is even wider when doing more repetitive searches and/or longer strings.

Code: Select all

DisableDebugger
Define test1.s,test2.s,elap.q,count.q,t1.q,t2.q
#Max = 1000000
test1="Dies ist ein cooler Test."
test2="test"

elap=ElapsedMilliseconds()
For count=1 To #Max
  If FindString(LCase(test1),test2)
  EndIf
Next
t1=ElapsedMilliseconds()-elap

elap=ElapsedMilliseconds()
For count=1 To #Max
  If FindString(test1,test2,1,#PB_String_NoCase)
  EndIf
Next
t2=ElapsedMilliseconds()-elap

EnableDebugger
Debug "LCase: "+Str(t1)+"ms"
Debug "NoCase: "+Str(t2)+"ms"
User avatar
NicTheQuick
Addict
Addict
Posts: 817
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: LCase faster than #PB_String_NoCase

Post by NicTheQuick »

Not on my system:
LCase: 138ms
NoCase: 93ms
I also tried this version without enabling the Debugger in the first place:

Code: Select all

Define test1.s,test2.s,elap.q,count.q,t1.q,t2.q
#Max = 10000000
test1="Dies ist ein cooler Test."
test2="test"

elap=ElapsedMilliseconds()
For count=1 To #Max
	If FindString(LCase(test1),test2)
	EndIf
Next
t1=ElapsedMilliseconds()-elap

elap=ElapsedMilliseconds()
For count=1 To #Max
	If FindString(test1,test2,1,#PB_String_NoCase)
	EndIf
Next
t2=ElapsedMilliseconds()-elap

result.s = "LCase: " + t1 + ~"ms\n" +
           "NoCase: " + t2 + "ms"

MessageRequester("Result", result)
It shows:
LCase: 1672ms
NoCase: 1059ms
By the way. The NoCase-Flag does not work with characters outside the English alphabet. Keep that in mind:

Code: Select all

Debug LCase("Ä") ;shows ä
Debug FindString("ä", "Ä", 1, #PB_String_NoCase); shows 0 (no match)
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
Paul
PureBasic Expert
PureBasic Expert
Posts: 1108
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: LCase faster than #PB_String_NoCase

Post by Paul »

@ jacdelad
Thanks, good to know.

Results on my system...
LCase: 93ms
NoCase: 394ms

Results with Debugger ON
LCase: 1596ms
NoCase: 4655ms
Image Image
User avatar
jacdelad
Enthusiast
Enthusiast
Posts: 139
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa
Contact:

Re: LCase faster than #PB_String_NoCase

Post by jacdelad »

@Nic:
I didn't know it makes a difference whether you use DisableDebugger or don't start it at all. I tried your version and got 2408ms/7418ms with LCase still much faster.
But...I definitely didn't think off special characters. Thanks for noticing. Maybe this can be improved by Fred. So, at the moment LCase seems to be the better choice at all.
AZJIO
Enthusiast
Enthusiast
Posts: 203
Joined: Sun May 14, 2017 1:48 am

Re: LCase faster than #PB_String_NoCase

Post by AZJIO »

NicTheQuick wrote: Fri Sep 24, 2021 5:08 pm By the way. The NoCase-Flag does not work with characters outside the English alphabet. Keep that in mind:
It works for the Russian language

Code: Select all

test1="Привет"
test2="привет"

; If FindString(LCase(test1),test2)
If FindString(test1,test2,1,#PB_String_NoCase)
	MessageRequester("yes ", "да")
Else
	MessageRequester("no", "нет")
EndIf
; LCase: 1496ms
; NoCase: 5889ms
Compiled, hoping it will be faster )))
; LCase: 1552ms
; NoCase: 5959ms
User avatar
NicTheQuick
Addict
Addict
Posts: 817
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: LCase faster than #PB_String_NoCase

Post by NicTheQuick »

It's interesting that only on my system the FindString variant is faster. Maybe it's because I am using Linux?
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.
#NULL
Addict
Addict
Posts: 1364
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: LCase faster than #PB_String_NoCase

Post by #NULL »

I'm on linux as well and NoCase is faster.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1108
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: LCase faster than #PB_String_NoCase

Post by Paul »

NicTheQuick wrote: Fri Sep 24, 2021 6:19 pm It's interesting that only on my system the FindString variant is faster. Maybe it's because I am using Linux?
Maybe.
These are my results on Linux (my other post was under Windows)
LCase: 142ms
NoCase: 64ms
Image Image
User avatar
mk-soft
Addict
Addict
Posts: 3190
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: LCase faster than #PB_String_NoCase

Post by mk-soft »

On macOS NoCase faster, but on windows very slow ...
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace
AZJIO
Enthusiast
Enthusiast
Posts: 203
Joined: Sun May 14, 2017 1:48 am

Re: LCase faster than #PB_String_NoCase

Post by AZJIO »

NicTheQuick
Since you have a different number of iterations, we compare different options.
1000000
10000000

Windows, 10e6 =
LCase: 150ms
NoCase: 600ms

Linux, 10e6 =
LCase: 261ms
NoCase: 104ms
NicTheQuick wrote: Fri Sep 24, 2021 5:08 pm By the way. The NoCase-Flag does not work with characters outside the English alphabet. Keep that in mind:
This rule works on Linux but not Windows
User avatar
jacdelad
Enthusiast
Enthusiast
Posts: 139
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa
Contact:

Re: LCase faster than #PB_String_NoCase

Post by jacdelad »

Oh, interesting. I did it on Windows too. Sorry for the confusion.
AZJIO
Enthusiast
Enthusiast
Posts: 203
Joined: Sun May 14, 2017 1:48 am

Re: LCase faster than #PB_String_NoCase

Post by AZJIO »

NicTheQuick wrote: Fri Sep 24, 2021 5:08 pm By the way. The NoCase-Flag does not work with characters outside the English alphabet. Keep that in mind:
Linux supports NoCase in regular expressions.

Code: Select all

If CreateRegularExpression(0, "Привет",  #PB_RegularExpression_NoCase)
	Debug MatchRegularExpression(0, "привет")
	Debug ReplaceRegularExpression(0, "привет", "---")
EndIf
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 322
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: LCase faster than #PB_String_NoCase

Post by ar-s »

LCase: 121ms
NoCase: 535ms
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x - W10 x64 - Ryzen 7 3700x #W10 / #ParrotOs

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
#NULL
Addict
Addict
Posts: 1364
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: LCase faster than #PB_String_NoCase

Post by #NULL »

The difference becomes a little less pronounced if you use LCase() for test2 as well, which would be necessary if test2 is in undefined case. And NoCase already covers that.
..Or in other words, if you know one of the strings is already in a specific case then you can use LCase() on the other one only, instead of using NoCase.
User avatar
jacdelad
Enthusiast
Enthusiast
Posts: 139
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa
Contact:

Re: LCase faster than #PB_String_NoCase

Post by jacdelad »

That makes sense. I guess if someone has to do a lot FindStringing it's good to test whether to LCase the strings beforehand, in the loop, or use #PB_String_NoCase. Depending how it's used.
Post Reply