第一个用时:0.015625
第二个用时:0
Option Explicit
Sub s1()
Dim i, s
Cells(1, 3) = Timer
s = 0
For i = 1 To 2500
s = s + Cells(i, 1)
Next i
Cells(1, 2) = s
Cells(1, 4) = Timer
End Sub
Sub s2()
Dim i, s, arr
Cells(1, 8) = Timer
s = 0
arr = Range("f1:f2500")
For i = 1 To 2500
s = s + arr(i, 1)
Next i
Cells(1, 7) = s
Cells(1, 9) = Timer
End Sub
直接访问11.719毫秒,使用数组3.906毫秒,减少了8毫秒,说当数据量低于100万的时候,提高的速度不到4秒。
为什么你的问题都是那么古怪 。。。。
都很快的按用秒计算的话都在1秒内。如果要更精确,只能先知道CPU的频率,根据CPU的速度来判定每步执行的速度了。应该没必要吧?
Private Sub press1_Click()
Sum = 0
time1 = Time
For i = 1 To 2500
Sum = Sum + Cells(i, 1)
Next
time2 = Time
Cells(1, 2) = Sum
Cells(2, 2) = time1
Cells(3, 2) = time2
End Sub
Private Sub press2_Click()
time1 = Time
Cells(1, 7) = Application.WorksheetFunction.Sum(Range("f1:f2500"))
time2 = Time
Cells(2, 7) = time1
Cells(3, 7) = time2
End Sub