|
楼主 |
发表于 2017-12-20 11:06
|
显示全部楼层
本帖最后由 Zamyi 于 2017-12-20 14:54 编辑
采用最极端的测试,Add过程改为:
- Public mm&
- Public Sub Add(Key As Variant, Item As Variant)
- Dim i&, R As Single
- k = Key Mod Hash_Count ' (hash(0, StrPtr(Key), LenB(Key)) And &H7FFFFFFF) Mod Hash_Count
- If pCount = BCount Then Call Dilate
- R = Rnd(pSeed)
- Do
- If Hash_Index(k) = 0 Then Exit Do
- If StrComp(Key, pKeys(Hash_Index(k))) = 0 Then MsgBox "该关键字已存在!": End
- k = (k + Hash_Count * Rnd) Mod Hash_Count
- mm = mm + 1
- Loop
- pCount = pCount + 1
- pKeys(pCount) = Key
- pItems(pCount) = Item
- Hash_Index(k) = pCount
- End Sub
复制代码
测试程序如下:- Public Sub test()
- Dim d As New zDict, i&, n&, a()
- n = 10000
- ReDim a(1 To n)
- Randomize
- For i = 1 To n
- a(i) = (i - 1) * n * 2 + 1
- Next
- t = Timer
- d.SetBudgetCount n
- For i = 1 To n
- d.Add a(i), i
- Next
- [e1] = Timer - t
- [f1] = d.mm/n
- End Sub
复制代码
相当于每增加一个不同Key,都是相同的k值,结果依然不出错,只是寻找达6000多次(1万Keys)。而一般随机数,查找仅为1.3~1.4次。说明Rnd还是可行的。
|
|