Day 024: LCASE$
#9
(12-04-2022, 06:47 PM)Pete Wrote: So brainstorming a bit here... which reminds me, buy Flex Tape...

The QB64 IDE keeps your entire code as a string, if I remember correctly. So I wanted to see what would be the faster way to search our code with LCASE$()

Code: (Select All)
' Method #1
a$ = "Dog Cat Rabbit Moose Elephant Bear Tiger Fish "
search$ = "moose"

IF INSTR(LCASE$(a$), search$) THEN PRINT "Found: "; search$ ELSE PRINT "No search results.."

' Method #2
DO
    j% = INSTR(seed%, a$, " ")
    IF j% = 0 THEN EXIT DO
    IF LCASE$(MID$(a$, seed%, j% - seed%)) = search$ THEN EXIT DO
    REM PRINT "|" + LCASE$(MID$(a$, seed%, j% - seed%)) + "|", search$ ' Show parsing...
    seed% = j% + 1
LOOP

IF j% THEN PRINT "Found: "; search$ ELSE PRINT "No search results.."

So I did some speed tests and it appears Method #1 is the winner by a factor of 5 or more.

Waiting for Steve to post... Why Method #3, of course!... and give the IDE method or somethong he came up with. (Spelling mistake intentional).

Pete

Method #3:  LCASE$ Once only

Code: (Select All)
' Method #1
a$ = "Dog Cat Rabbit Moose Elephant Bear Tiger Fish "
search$ = "moose"

If InStr(LCase$(a$), search$) Then Print "Found: "; search$ Else Print "No search results.."


' Method #2
Do
    j% = InStr(seed%, a$, " ")
    If j% = 0 Then Exit Do
    If LCase$(Mid$(a$, seed%, j% - seed%)) = search$ Then Exit Do
    Rem PRINT "|" + LCASE$(MID$(a$, seed%, j% - seed%)) + "|", search$ ' Show parsing...
    seed% = j% + 1
Loop

If j% Then Print "Found: "; search$ Else Print "No search results.."

'method #3: 'the LCASE$ once method

b$ = LCase$(a$)
Do
    j% = InStr(seed%, b$, " ")
    If j% = 0 Then Exit Do
    If Mid$(b$, seed%, j% - seed%) = search$ Then Exit Do
    Rem PRINT "|" + LCASE$(MID$(a$, seed%, j% - seed%)) + "|", search$ ' Show parsing...
    seed% = j% + 1
Loop
b$ = "" 'free up all that massively backed up data, since this isn't in a SUB/FUNCTION which woul do so for us at exit time

If j% Then Print "Found: "; search$ Else Print "No search results.."

The QB64 IDE has a slightly more complex structure than what you're using.  It stores all your info as:  length, data, length

So your example data would be:

"3Dog33Cat36Rabbit68Elephant8..."

So to start with, it'd read your length of 3, then 3bytes for "Dog", and it'd skip forward one to bypass the second "3".
For the next line in the IDE, it'd read the length of 3, the 3 bytes for "Cat", and then skip forward over the next "3".

So why does it have that extra 3, or 6, or whatever the word length is in there, if it's just going to skip it??

For searching/reading in a backwards order.  Start from the right, read the count, then back up and read that number of letters.



One thing to keep in mind with your comparison methods -- you're comparing apples and apple sauce.

Method one simply looks for a pattern inside a whole string.  If you had your buddy, "Mooser", in the list, the search would find him and flag him for "moose"  Now, of course, you could change your search term to "moose ", but then you'd miss out on "moose, cat, dog"...   So now you have to expand to search for term + break characters, only to get glitched up when moose comes at the end of a line and is mo-ose separated..."  So now you have to look for hypens and determine if they're concatenators or just dashes or maybe subtraction or negative symbols...

Whereas method two breaks things down to individual elements to begin with.  In this case, you're using spaces as valid separators, but that's easily expanded to add what characters you need.  Space + ",.;-_" + any others you feel are valid...

So which method is faster at the end of the day?  I'd say that all depends on how complex your use-case is with your code.  Sometimes you can get away with something as simple as efficient as Pete's Example #1.  Other times, your data requires much more careful analysis, and in those cases, I'd find it impossible to declare *any* method the fastest, without having the specific data and test methods in front of me to compare actual results on.  Wink
Reply


Messages In This Thread
Day 024: LCASE$ - by Pete - 12-04-2022, 05:17 AM
RE: Day 024: LCASE$ - by mnrvovrfc - 12-04-2022, 12:01 PM
RE: Day 024: LCASE$ - by luke - 12-04-2022, 01:25 PM
RE: Day 024: LCASE$ - by SMcNeill - 12-04-2022, 01:48 PM
RE: Day 024: LCASE$ - by mnrvovrfc - 12-04-2022, 05:57 PM
RE: Day 024: LCASE$ - by SMcNeill - 12-04-2022, 10:47 PM
RE: Day 024: LCASE$ - by Pete - 12-04-2022, 04:10 PM
RE: Day 024: LCASE$ - by Pete - 12-04-2022, 06:47 PM
RE: Day 024: LCASE$ - by SMcNeill - 12-04-2022, 11:16 PM
RE: Day 024: LCASE$ - by Pete - 12-05-2022, 12:59 AM
RE: Day 024: LCASE$ - by luke - 12-05-2022, 07:30 AM
RE: Day 024: LCASE$ - by mnrvovrfc - 12-05-2022, 08:24 AM
RE: Day 024: LCASE$ - by Pete - 12-05-2022, 10:38 AM



Users browsing this thread: 4 Guest(s)