即使這篇文章已有四年歷史了,我對(duì)此感到好奇并進(jìn)行了一些測試。最重要的是要注意編碼器應(yīng)該始終將變量聲明為SOMETHING。未申報(bào)的變量顯然表現(xiàn)最差(技術(shù)上未申報(bào)Variant
)
Long
確實(shí)表現(xiàn)最快,所以我不得不認(rèn)為微軟總是建議使用Long
而不是Integer
有意義。我猜測也是如此Byte
,但大多數(shù)程序員都沒有使用它。
64位WINDOWS 10 LAPTOP的結(jié)果
使用代碼:
Sub VariableOlymics()'Run this macro as many times as you'd like, with an activesheet ready for data'in cells B2 to D6Dim beginTIME As
Double, trials As Long, i As Long, p As Long
trials = 1000000000
p = 0
beginTIME = Now For i = 1 To trials Call boomBYTE Next i Call Finished(p, Now - beginTIME, CDbl(trials))
p = p + 1
beginTIME = Now For i = 1 To trials Call boomINTEGER Next i Call Finished(p, Now - beginTIME, CDbl(trials))
p = p + 1
beginTIME = Now For i = 1 To trials Call boomLONG Next i Call Finished(p, Now - beginTIME, CDbl(trials))
p = p + 1
beginTIME = Now For i = 1 To trials Call boomDOUBLE Next i Call Finished(p, Now - beginTIME, CDbl(trials))
p = p + 1
beginTIME = Now For i = 1 To trials Call boomUNDECLARED Next i Call Finished(p, Now - beginTIME, CDbl(trials))
p = p + 1End SubPrivate Sub boomBYTE()Dim a As Byte, b As Byte, c As Byte
a = 1
b = 1 + a
c = 1 + b
c = c + 1End SubPrivate Sub boomINTEGER()Dim a As Integer, b As Integer, c As Integer
a = 1
b = 1 + a
c = 1 + b
c = c + 1End SubPrivate Sub boomLONG()Dim a As Long, b As Long, c As Long
a = 1
b = 1 + a
c = 1 + b
c = c + 1End SubPrivate Sub boomDOUBLE()Dim a As Double, b As Double, c As Double
a = 1
b = 1 + a
c = 1 + b
c = c + 1End SubPrivate Sub boomUNDECLARED()
a = 1
b = 1 + a
c = 1 + b
c = c + 1End SubPrivate Sub Finished(i As Long, timeUSED As Double, trials As Double)
With Range("B2").Offset(i, 0)
.Value = .Value + trials .Offset(0, 1).Value = .Offset(0, 1).Value + timeUSED
.Offset(0, 2).FormulaR1C1 = "=ROUND(RC[-1]*3600*24,0)"
End WithEnd Sub