|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
以下2个子过程,都是循环1000万次,做的事情也完全一样。只是前一个过程( test2)是两个循环先完成第一个,再完成第二个;后一个过程( test3)是,把1000万次循环分成100000份,外循环100000次,2个内循环都是100次——减少了内存的占用。
test2在我的电脑上执行时间0.9秒, test3在我的电脑上执行时间2.2秒,速度相差2倍多。在实际应用中,由于外循环内的子循环更多,按test3的结构编程速度相差(原来想当然写成100倍,计算一下)25倍以上(但是有不得不按test3的结构编程)。
为什么会这样呢?期待高手参加讨论,并进行指点。
晕死! 此贴上一个过程的timer放置的位置错误,导致白白浪费大家的经历,此贴不在讨论。谢谢大家。
按这个速度计算我的实际应用程序还可以快12-13倍,但是我没有找到问题的症结,找到症结后另发新帖- Sub test2()
- Dim bHappy(10000000) As Long
- Dim barrFailure(10000000) As Long
- Dim barrWinning(10000000) As Long
- Dim WinningCount As Long, FailureCount As Long, i As Long
- Randomize
- For i = 0 To 10000000
- bHappy(i) = CLng(Rnd * 10000000) - CLng(Rnd * 10000000)
- Next
- Dim t As Single
- t = Timer
- For i = 0 To 10000000
- If bHappy(i) > 0 Then
- WinningCount = WinningCount + 1
- If FailureCount > 0 Then
- barrFailure(FailureCount) = barrFailure(FailureCount) + 1
- FailureCount = 0
- End If
- Else
- FailureCount = FailureCount + 1
- If WinningCount > 0 Then
- barrWinning(WinningCount) = barrWinning(WinningCount) + 1
- WinningCount = 0
- End If
- End If
- Next
- t = Timer - t
- MsgBox CStr(t)
- End Sub
- Sub test3()
- Dim bHappy(100) As Long
- Dim barrFailure(100) As Long
- Dim barrWinning(100) As Long
- Dim WinningCount As Long, FailureCount As Long, i As Long, j&
- Dim t As Single
-
- t = Timer
- For j = 0 To 100000
- Randomize
- For i = 0 To 100
- bHappy(i) = CLng(Rnd * 100) - CLng(Rnd * 100)
- Next
- For i = 0 To 100
- If bHappy(i) > 0 Then
- WinningCount = WinningCount + 1
- If FailureCount > 0 Then
- barrFailure(FailureCount) = barrFailure(FailureCount) + 1
- FailureCount = 0
- End If
- Else
- FailureCount = FailureCount + 1
- If WinningCount > 0 Then
- barrWinning(WinningCount) = barrWinning(WinningCount) + 1
- WinningCount = 0
- End If
- End If
- Next
- Next
-
- t = Timer - t
- MsgBox CStr(t)
- End Sub
复制代码
[ 本帖最后由 老伙计2008 于 2009-12-19 09:57 编辑 ] |
|