a$ = "this is uncopyrightable"
FOR i% = 1 TO 26
FOR j% = 1 TO LEN(a$)
t$ = UCASE$( MID$(a$,j%,1) )
IF ASC(t$) = 64 + i% THEN PRINT t$;
NEXT
NEXT
@CharlieJV almost same as Pete's only without instr both suffer from taking ucase$ or lcase$ more often than needed, asc can take a 2nd argument and work faster than mid$ for single letter.
Code: (Select All)
a$ = "this is uncopyrightable"
ua$ = UCase$(a$) ' do this once
For i% = 1 To 26
For j% = 1 To Len(a$)
If Asc(ua$, j%) = 64 + i% Then Print Chr$(i% + 64);
Next
Next
i think instr will go faster than testing every letter in word 26 times.
i am suspecting steve's or my mod of anacode$ with string$ will be faster than pete's using instr in alpha order, i like the fact we count over the length of word only once and there is no if testing when producing the return string from counts array. it is count len(word$) + 26 string alpha steps versus instr length of word and if find instr again and another if find... length of word x 2 ifs over 26 letters steps ie they both have to go 26 letter steps but no if's in steve's/my mod with string$.
i will test by anacoding a dictionary with both methods, might consider throwing out the need for ucase$/lcase$ as entire dictionary is all capitals to start.
Yeah, I could have called UCASE$ once outside of the looping, but I was going for shortest amount of code vs performance.
That said, UCASE$ x times for a single character vs UCASE$ 1 time for x characters, would there be a difference?
Yeah, not in love with that second parameter for ASC. I'm a little bit too much old school there.
yeah strings process slower than number specially integers or long, so asc is way faster than mid$ and the more you use functions specially in loop the longer or less efficient the code, i know we are talking tiny tiny bits of time but every little bit adds up.
04-25-2024, 01:56 PM (This post was last modified: 04-25-2024, 01:57 PM by bplus.)
and the results are in, Petecode$ just edges out 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$
a$(i) = AnaCode$(w$)
Wend
Close
Next
anatime = 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$
p$(i) = AnaCode$(w$)
Wend
Close
Next
petetime = Timer(.01) - start
Print anatime, petetime
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
' return sorted ancagram code string for any word, call ucase$(wrd$) for all caps FunctionAnaCode$ (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 DimAsInteger L(65To91), i, p Dim rtn$ For i = 1ToLen(wrd$)
p = Asc(wrd$, i)
L(p) = L(p) + 1 Next
l = 1
rtn$ = wrd$ 'to set proper length for rtn$ For i = 65To91 If L(i) Then'only do the string junk if a letter exists Mid$(rtn$, l) = String$(L(i), i) ' thanks steve for string$ idea -- You're welcome... Here's another idea: don't add strings as that's slow too.
l = l + L(i) End If Next AnaCode$ = rtn$ End Function
FunctionpeteCode$ (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% = 65To91
seed% = InStr(a$, Chr$(i%)) While seed%
rtn$ = rtn$ + Chr$(i%)
seed% = InStr(seed% + 1, a$, Chr$(i%)) Wend Next peteCode$ = rtn$ End Function
I didn't have your word list, so I substituted one of my own, which I think most folks around here should have a copy of.
And in other news::
(04-25-2024, 01:56 PM)bplus Wrote: and the results are in, Petecode$ just edges out 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$
a$(i) = AnaCode$(w$)
Wend
Close
Next
anatime = 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$
p$(i) = AnaCode$(w$)
Wend
Close
Next
petetime = Timer(.01) - start
Print anatime, petetime
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
Note that the code you've shared above runs the same subroutine for BOTH timers. You never actually call peteCode anywhere...
04-25-2024, 05:25 PM (This post was last modified: 04-25-2024, 05:28 PM by bplus.)
rerun
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$
a$(i) = AnaCode$(w$)
Wend
Close
Next
anatime = 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$
p$(i) = peteCode$(w$)
Wend
Close
Next
petetime = Timer(.01) - start
Print anatime, petetime
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
peteCode slightly ahead, I am going to reverse positions and teach myself to use other shift key!
BTW my word file is old unixdict.txt file with all words uning non letters like numbers of single quote removed and words CAPITALIZED.
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.
04-25-2024, 06:16 PM (This post was last modified: 04-25-2024, 06:20 PM by bplus.)
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
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.
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.