QB64 Phoenix Edition
How to format a While... Wend correctly? - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: How to format a While... Wend correctly? (/showthread.php?tid=2466)

Pages: 1 2 3 4 5


RE: How to format a While... Wend correctly? - SMcNeill - 02-29-2024

Attach it in 7z or zip format, if you can.  Wink


RE: How to format a While... Wend correctly? - PhilOfPerth - 02-29-2024

How 'barrassment!
I was shown how to attach files once before, but can't find a way to do this again now!
Wasn't there an "attach file" button somewhere?


RE: How to format a While... Wend correctly? - Jack - 02-29-2024

PhilOfPerth, use "New Reply" and not the "Quick Reply"
at the bottom of a new reply you will see the attachment option


RE: How to format a While... Wend correctly? - PhilOfPerth - 02-29-2024

Ah yes! Thanks Jack.
Maybe the option should be available in Quick Reply too?
Anyway, there it is. Have fun, Steve!


RE: How to format a While... Wend correctly? - SMcNeill - 02-29-2024

I submit the following for a starting point for a speed test for folks to compare against:

Code: (Select All)
dict$ = "Collins.txt"

Screen _NewImage(800, 600, 32)

Type Dict_Type
As String Word, Definition
End Type

ReDim Shared Dict(1000000) As Dict_Type
Open dict$ For Binary As #1
Do Until EOF(1)
Line Input #1, temp$
l = InStr(temp$, Chr$(9)) 'tab separated data file
count = count + 1
Dict(count).Word = _Trim$(Left$(temp$, l - 1))
Dict(count).Definition = _Trim$(Mid$(temp$, l + 1))
Loop
Close

ReDim _Preserve Dict(count) As Dict_Type

Dim Junk(10) As String
Data cheese,dog,cat,elephant,rootbeer,house,food,drink,zebra,mouse
For i = 1 To 10
Read Junk(i)
Next

t# = Timer
For i = 1 To 10
f = FindWord(Junk(i))

Print Junk(i),
If f = 0 Then
Print "Word not found"
Else
Print Dict(FindWord(Junk(i))).Definition
End If
Next
t1# = Timer
Print Using "###.######## seconds to find #### words and definitions"; t1# - t#, i - 1

Function FindWord (word$)
Dim As Long low, hi, test
low = 1: hi = UBound(Dict)
While low <= hi
test = Int((low + hi) / 2)
Select Case _StriCmp(Dict(test).Word, word$)
Case 0
'Print "found"; test
FindWord = test: Exit Function
Case -1
'Print "low"; Dict(test).Word
low = test + 1
Case 1
'Print "high"; Dict(test).Word
hi = test - 1
End Select
Wend
End Function


Quick question though: Are you looking for the fastest way to LOAD the file, or the fastest way to SEARCH the file?

I didn't really try to push the limits of either here, as I'm certain I can do both load and search faster with just very little effort. I just thought I'd start with a simple OPEN FOR BINARY and LINE INPUT to get our data, and then a simple binary search to find the words, so folks would have a starting point to compete against.

Only thing is, I wasn't certain what exactly I was timing, so the above only times search times for me, and on my PC that's 0.0 seconds for 10 word look-ups.

Looks like we need a bigger search list to go on as well! LOL!


RE: How to format a While... Wend correctly? - SMcNeill - 02-29-2024

I finally have results for how long a binary search method takes!

Code: (Select All)
dict$ = "Collins.txt"

Screen _NewImage(800, 600, 32)

Type Dict_Type
As String Word, Definition
End Type

ReDim Shared Dict(1000000) As Dict_Type
Open dict$ For Binary As #1
Do Until EOF(1)
Line Input #1, temp$
l = InStr(temp$, Chr$(9)) 'tab separated data file
count = count + 1
Dict(count).Word = _Trim$(Left$(temp$, l - 1))
Dict(count).Definition = _Trim$(Mid$(temp$, l + 1))
Loop
Close

ReDim _Preserve Dict(count) As Dict_Type

Dim Junk(10) As String
Data cheese,dog,cat,elephant,rootbeer,house,food,drink,zebra,mouse
For i = 1 To 10
Read Junk(i)
Next


t# = Timer
For k = 1 To 10000
For i = 1 To 10
f = FindWord(Junk(i))
If k = 1 Then 'no need to scroll the screen and print the words repeatedly
If f = 0 Then
Print Junk(i), "Word not found"
Else
Print Junk(i), Dict(FindWord(Junk(i))).Definition
End If
End If
Next
Next
t1# = Timer

Print Using "###.######## seconds to find #### words and definitions"; t1# - t#, i - 1

Function FindWord (word$)
Dim As Long low, hi, test
low = 1: hi = UBound(Dict)
While low <= hi
test = Int((low + hi) / 2)
Select Case _StriCmp(Dict(test).Word, word$)
Case 0
'Print "found"; test
FindWord = test: Exit Function
Case -1
'Print "low"; Dict(test).Word
low = test + 1
Case 1
'Print "high"; Dict(test).Word
hi = test - 1
End Select
Wend
End Function

We search for 10 words, 10000 times, for a total of 100,000 word look-ups...

and it takes 0.05 seconds on my PC.

That's not bad for a starting benchmark, I don't think. Big Grin


RE: How to format a While... Wend correctly? - bplus - 02-29-2024

For me the main concern was not taking anytime to load the word file let alone the word and definitions file at the start of the game.

The Binary search does not take a noticable amount of time for a word lookup, the human player is way too slow to need a slew of words searched at once. Maybe different with a spellchecker.


RE: How to format a While... Wend correctly? - SpriggsySpriggs - 02-29-2024

I've never been a fan of automatically assuming an array size for a file's contents. I'd rather just count the number of items in the file and then build the array or make it dynamic and resize it for each new item.


RE: How to format a While... Wend correctly? - SMcNeill - 02-29-2024

(02-29-2024, 01:09 PM)SpriggsySpriggs Wrote: I've never been a fan of automatically assuming an array size for a file's contents. I'd rather just count the number of items in the file and then build the array or make it dynamic and resize it for each new item.

It resizes itself after the DO....LOOP.  Wink


RE: How to format a While... Wend correctly? - SpriggsySpriggs - 02-29-2024

I'm talking about resizing up to allow for more than 1,000,000. Not that you would ever need to, but I just dislike rigidity in data capture.