Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alphabetical sort of characters within a string.
#29
yeah see i reverse which routine runs first and anacode is tiny bit faster so disadvantage which ever routine is run first but petetime was better over anacode
Code: (Select All)
_Title "anaCode$ versus peteCode$ with dictionary" ' b+ 2024-04-25 speed test
Dim a$(25000), p$(25000)
Dim i As Integer
start = Timer(.01)
For i = 1 To 25
    Open "WORDS.txt" For Input As #1
    While Not EOF(1)
        Input #1, w$
        p$(i) = peteCode$(w$)
    Wend
    Close
Next
petetime = Timer(.01) - start

start = Timer(.01)
For i = 1 To 25
    Open "WORDS.txt" For Input As #1
    While Not EOF(1)
        Input #1, w$
        a$(i) = AnaCode$(w$)
    Wend
    Close
Next
anatime = Timer(.01) - start

Print "PeteTime ="; petetime, "AnaTime ="; anatime
For i = 1 To 25000
    If a$(i) <> p$(i) Then Print a$(i), p$(i)
Next
Print "END OF DIFF CHECK"



' return sorted ancagram code string for any word, call ucase$(wrd$) for all caps
Function AnaCode$ (wrd$) ' anaCode$ converts word to an Anagram pattern
    ' wrd$ is assumed to be in all capitals!!!
    ' number of A's in first, number of B's in 2nd, number of C's in third
    Dim As Integer L(26), i, p
    Dim rtn$
    For i = 1 To Len(wrd$)
        p = Asc(wrd$, i) - 64 ' A=1, B=2...
        L(p) = L(p) + 1
    Next
    For i = 1 To 26
        rtn$ = rtn$ + String$(L(i), Chr$(i + 64)) ' thanks steve for string$ idea
    Next
    AnaCode$ = rtn$
End Function

Function peteCode$ (a$) 'converts word to an Anagram pattern
    ' a$ is assumed to be all caps call ucase$(a$) if not
    Dim i%, seed%, rtn$
    For i% = 1 To 26
        seed% = InStr(a$, Chr$(64 + i%))
        While seed%
            rtn$ = rtn$ + Chr$(64 + i%)
            seed% = InStr(seed% + 1, a$, Chr$(64 + i%))
        Wend
    Next
    peteCode$ = rtn$
End Function

   
best beat time for anacode running 2nd was .3 secs whereas best beat time petecode ran was consistely around .6 secs

btw here is WORD.txt file if someone else wants to try this, < 25,000 words

(04-25-2024, 05:42 PM)SMcNeill Wrote: 2.5 secs (modified ana) vs 2.9 secs (pete).

So it goes from a little slower than pete's to about 20% faster than it, on my machine.  I dunno how large a difference you consider that to be, though I'd call it worth noting.  Smile

ok i will try your optimization with my test code and system, 65 to 90 stuff

it is wierd that which ever goes first is slower on my tests.


Attached Files
.txt   WORDS.txt (Size: 225.2 KB / Downloads: 7)
b = b + ...
Reply


Messages In This Thread
RE: Alphabetical sort of characters within a string. - by bplus - 04-25-2024, 06:16 PM



Users browsing this thread: 4 Guest(s)