四种整数数据类型的性能对比

在我们写VBA程序的时候,我们经常要面对数据类型定义的选择,有的情况下,业务本身对于数据类型有要求和限制,那么我们并不难以选择,有些时候却没有限制,我们可以任意选用四种整数类型(Byte,Integer,Long,Currency)中的一种,例如:

> For i=1 to 100

在这行代码中,我们该把变量i定义为什么类型的变量呢?显然四种整数类型都可以正常运行,但是他们的效率是否相同呢?我们到底该如何选择?有的人说,当时是选最小的数据类型Byte,有的人说在32位系统上,32位的Long类型才是效率最高的。

那么究竟谁说的是正确的,让我们来进行一下这四种整数类型的性能对比测试,我们使用如下代码:

> Const LoopTimes = 100000000 > > Public Sub test()
> Dim bytTmp As Byte
> Dim intTmp As Integer
> Dim lngTmp As Long
> Dim curTmp As Currency
> Dim loopCount As Long
>
> Dim timeBegin As Single
> Dim timeEnd As Single
> Dim timeAddition As Single
>
> timeBegin = Timer
> For loopCount = 0 To LoopTimes
> Next loopCount
> timeEnd = Timer
> timeAddition = timeEnd - timeBegin
>
>
> timeBegin = Timer
> For loopCount = 0 To LoopTimes
> bytTmp = 255
> Next loopCount
> timeEnd = Timer
> Debug.Print "Byte :"; timeEnd - timeBegin - timeAddition; "秒" > > timeBegin = Timer
> For loopCount = 0 To LoopTimes
> intTmp = 255
> Next loopCount
> timeEnd = Timer
> Debug.Print "Integer :"; timeEnd - timeBegin - timeAddition; "秒" > > timeBegin = Timer
> For loopCount = 0 To LoopTimes
> lngTmp = 255
> Next loopCount
> timeEnd = Timer
> Debug.Print "Long :"; timeEnd - timeBegin - timeAddition; "秒" > > timeBegin = Timer
> For loopCount = 0 To LoopTimes
> curTmp = 255
> Next loopCount
> timeEnd = Timer
> Debug.Print "Currency :"; timeEnd - timeBegin - timeAddition; "秒"
> Debug.Print "*********************" > > End Sub

在这里,我们对每个整数类型进行了1亿次的赋值操作,同时减去了循环控制所消耗的空转时间,剩下的就是纯粹的赋值操作所需的时间。最后,让我们来看看运行的结果:

> Byte : 7.234375 秒
> Integer : 2.421875 秒
> Long : 3.4375 秒
> Currency : 4.84375 秒
> *********************
> Byte : 7.234375 秒
> Integer : 2.421875 秒
> Long : 3.453125 秒
> Currency : 4.875 秒
> *********************
> Byte : 7.21875 秒
> Integer : 2.421875 秒
> Long : 3.421875 秒
> Currency : 4.875 秒
> *********************

看到这里,我想大家都应该很清楚了,虽然Byte占用内存最少,但是他的性能却是最差的,如果对于单个变量,我们没有必要使用Byte,当然Byte在大块数据段进行指针操作的时候,还是有他的非凡之处。剩下三种整数数据类型里面,Integer性能最佳,Currency性能最差。我们尽可能选择能够满足我们业务需要的最小数据类型。

上面是赋值操作的性能对比,下面我们来进行位操作的性能对比测试,我们使用如下代码:

> Const LoopTimes = 10000000 > > Public Sub test()
> Dim bytTmp As Byte
> Dim intTmp As Integer
> Dim lngTmp As Long
> Dim curTmp As Currency
> Dim strTmp As String
> Dim loopCount As Long
>
> Dim timeBegin As Single
> Dim timeEnd As Single
> Dim timeAddition As Single
>
> timeBegin = Timer
> For loopCount = 0 To LoopTimes
> strTmp = 255
> Next loopCount
> timeEnd = Timer
> timeAddition = timeEnd - timeBegin
>
>
> timeBegin = Timer
> For loopCount = 0 To LoopTimes
> strTmp = bytTmp Or 255
> Next loopCount
> timeEnd = Timer
> Debug.Print "Byte :"; timeEnd - timeBegin - timeAddition; "秒" > > timeBegin = Timer
> For loopCount = 0 To LoopTimes
> strTmp = intTmp Or 255
> Next loopCount
> timeEnd = Timer
> Debug.Print "Integer :"; timeEnd - timeBegin - timeAddition; "秒" > > timeBegin = Timer
> For loopCount = 0 To LoopTimes
> strTmp = lngTmp Or 255
> Next loopCount
> timeEnd = Timer
> Debug.Print "Long :"; timeEnd - timeBegin - timeAddition; "秒" > > timeBegin = Timer
> For loopCount = 0 To LoopTimes
> strTmp = curTmp Or 255
> Next loopCount
> timeEnd = Timer
> Debug.Print "Currency :"; timeEnd - timeBegin - timeAddition; "秒"
> Debug.Print "*********************" > > End Sub

这里,我们所比较的是逐位或操作,同样我们扣除了循环控制时间,赋值时间,下面让我们来看看结果:

> Byte : .625 秒
> Integer : .296875 秒
> Long : .296875 秒
> Currency : .890625 秒
> *********************
> Byte : .609375 秒
> Integer : .34375 秒
> Long : .328125 秒
> Currency : .90625 秒
> *********************
> Byte : .484375 秒
> Integer : .265625 秒
> Long : .203125 秒
> Currency : .8125 秒
> *********************
> Byte : .53125 秒
> Integer : .328125 秒
> Long : .28125 秒
> Currency : .875 秒
> *********************

我们可以看到,在位操作项目上,Byte赶上了Currency成了第三名,而Integer和Long则咬得很紧,但是最终还是Long胜出了,看来在32位系统上,32位数据类型确实有位操作上的优势,不要以为1字节位操作就会比4字节位操作快,事实上正好相反,4字节>2字节>1字节。

综合以上表现,我们的结论是,Byte和Currency的表现是最差的,但是这两个数据类型有他们的特殊用途,Byte适用于内存块的批量操作,Currency适用数据类型不确定的时候,剩下的Integer和Long,Integer在赋值操作上更快,Long在位操作上更快。

现在你知道该如何选择整数数据类型了吗?

Published At
Categories with Web编程
Tagged with
comments powered by Disqus