|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 m_n_j001 于 2013-11-21 15:44 编辑
今天学习了一下Collection的使用,看到一位达人在帖子中说大意是数据量大的情况下用Collection至少能快不少。怎么没感觉到呢?如以下三个生成不重复数据的代码:
Sub aaa() '这个用数组,很快
t = Timer
Dim ar(1 To 65536, 1 To 1), br(1 To 65536)
For i = 1 To 65536
br(i) = i
Next
Randomize
For i2 = 1 To 65536
i = i - 1
r = Int(Rnd * i + 1)
ar(i, 1) = br(r)
br(r) = br(i)
Next
[a1] = Timer - t
[b1:b65536] = ar
End Sub
Sub bbb() '这个用字典,稍慢。如果和集合一样改成remove,则成老态龙钟的蜗牛
t = Timer
Dim Dic As New Dictionary
Dim ar(1 To 65536, 1 To 1)
Dim i As Long
For i = 1 To 65536
Dic(i) = i
Next
Randomize
For i2 = LBound(ar) To UBound(ar)
i = i - 1
r = Int(Rnd * i + 1)
ar(i2, 1) = Dic(r)
Dic(r) = Dic(i)
Next
[a2] = Timer - t
[c1:c65536] = ar
End Sub
Sub ccc() '这个用集合,很慢,估计是Remove造成的
t = Timer
Dim s As New Collection, ar(1 To 65536, 1 To 1)
For i = 1 To 65536
s.Add i
Next
Randomize
For i = 1 To 65536
r = Int(Rnd * s.Count + 1)
ar(i, 1) = s(r)
s.Remove (r)
Next
[a3] = Timer - t
[d1:d65536] = ar
End Sub
这还是在数万个数据情况,要是再多呢?怎么体现“数据量大的情况下用Collection至少能快不少”?是哪里错了?请各位达人指教!或提供一些示例体现Collection的快,谢谢
---------------------------------------------------------------------------------------------------------------------------------------
在众多达人的帮助下,特别是14楼达人的回复,从根源上指明了原理,非常感谢!现在基本可以结贴了。
此贴学习笔记:
集合中,访问内容时用key远胜于index,删除时从前删远胜于从后删。
|
|