我已经用简写符号声明了一个整数变量 testInt,如下所示
Dim testInt%
使用有什么区别吗
somevalue = testInt * testInt
相对
somevalue = testInt% * testInt%
简而言之,在引用变量的每一点都使用类型说明符是否有优势?
一个快速的时间测试表明它们是线球——这在直觉上是有道理的
使用Long
而不是Integer
会更有效。请参阅此MSFT 链接
我将使用更准确的 API 计时器重复此操作
Sub B()
Dim testInt%
Dim somevalue%
Dim lcnt As Long
Dim dbStart As Double
dbStart = Timer()
For lcnt = 1 To 100000000
somevalue = testInt * testInt
Next
MsgBox "Time was " & Timer() - dbStart
End Sub
Sub A()
Dim testInt%
Dim somevalue%
Dim lcnt As Long
Dim dbStart As Double
dbStart = Timer()
For lcnt = 1 To 100000000
somevalue = testInt% * testInt%
Next
MsgBox "Time for type-specified was " & Timer() - dbStart
End Sub