|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本地变量(过程变量)、公共变量和传递变量的运算速度是不同的,本地变量运算速度最快,为传递变量运算速度的4倍左右,公共变量略慢于本地变量
下面示例在我的电脑上运行时间分别为1.453秒(本地)、1.79秒(公共)和6.156秒(传递)- Option Explicit
- Dim x As Long
- Sub test1()
- Dim i As Long
- Dim j As Long
- Dim t As Single
- i = 0: j = 0
- t = Timer
- For j = 1 To 100000000
- i = i + 1
- Next
- t = Timer - t
- MsgBox CStr(t) & " i=" & CStr(i)
- i = 0: j = 0
- t = Timer
- For j = 1 To 100000000
- x = x + 1
- Next
- t = Timer - t
- MsgBox CStr(t) & " i=" & CStr(i)
- i = 0
- test2 i
- End Sub
- Sub test2(i)
- Dim j As Long
- Dim t As Single
- t = Timer
- For j = 1 To 100000000
- i = i + 1
- Next
- t = Timer - t
- MsgBox CStr(t) & " i=" & CStr(i)
- End Sub
复制代码 |
|