Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to format a While... Wend correctly?
#16
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
Reply


Messages In This Thread
RE: How to format a While... Wend correctly? - by SMcNeill - 02-29-2024, 11:14 AM



Users browsing this thread: 13 Guest(s)