http://club.excelhome.net/viewthread.php?tid=274302&replyID=&skin=0 讨论]多列中查找重复值的最优方案 高手们的讨论精彩绝伦,看完对二个代码的速度不点不解? 狼版代码: Sub macro111() Dim arr, i As Long, n As Long, b(1000000) As Byte, t As Single Application.ScreenUpdating = False arr = [A1:d60000] t = Timer For i = 1 To 60000 b(arr(i, 1)) = 1 Next For i = 1 To 60000 If b(arr(i, 2)) = 1 Then b(arr(i, 2)) = 2 Next For i = 1 To 60000 If b(arr(i, 3)) = 2 Then n = n + 1: arr(n, 4) = arr(i, 3) Next [d1].Resize(n, 1) = Application.Index(arr, 0, 4) Application.ScreenUpdating = True MsgBox Timer - t & "秒!" End Sub ldy888代码: Sub Macro22() Dim arr, i As Long, n As Long, b(1000000) As Boolean, c(1000000) As Boolean, t As Single Application.ScreenUpdating = False Dim d As New Dictionary ' Set d = CreateObject("Scripting.Dictionary") arr = Range("a1:c60000") t = Timer For i = 1 To 60000 b(arr(i, 1)) = True Next For i = 1 To 60000 If b(arr(i, 2)) Then c(arr(i, 2)) = True Next For i = 1 To 60000 If c(arr(i, 3)) Then d(arr(i, 3)) = 1 Next [d1].Resize(d.Count, 1) = Application.Transpose(d.Keys) Application.ScreenUpdating = True MsgBox Timer - t & "秒!" '最快到到 0.0625秒 End Sub 以上二个代码循环道数一样,第二个代码比第一个多了个c(1000000) As Boolean数组和一个书典对象, 从占用内存上和按照访问对象越频繁速度越慢的原则,第二个代码应该更慢才对,但事实上是第二个代码更快,不得其解?水平有限,但学习心切,所以发个贴,望高手不吝解释解释,为谢!
[此贴子已经被作者于2007-11-6 14:18:28编辑过] |