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

在我们写VBA程序的时候,我们经常要面对数据类型定义的选择,有的情况下,业务本身对于数据类型有要求和限制,那么我们并不难以选择,有些时候却没有限制,我们可以任意选用四种整数类型(Byte,Integer,Long,Currency)中的一种,例如:For i=1 to 100在这行代码中,我们该把变量i定义为什么类型的变量呢?显然四种整数类型都可以正常运行,但是他们的效率是否相同呢?我们到底该如何选择?有的人说,当时是选最小的数据类型Byte,有的人说在32位系统上,32位的Long类型才是效率最高的。那么究竟谁说的是正确的,让我们来进行一下这四种整数类型的性能对比测试,我们使用如下代码:Const LoopTimes = 100000000Public Sub test()Dim bytTmp As ByteDim intTmp As IntegerDim lngTmp As LongDim curTmp As CurrencyDim loopCount As LongDim timeBegin As SingleDim timeEnd As SingleDim timeAddition As SingletimeBegin = TimerFor loopCount = 0 To LoopTimesNext loopCounttimeEnd = TimertimeAddition = timeEnd – timeBegintimeBegin = TimerFor loopCount = 0 To LoopTimesbytTmp = 255Next loopCounttimeEnd = TimerDebug.Print “Byte :”; timeEnd – timeBegin – timeAddition; “秒”timeBegin = TimerFor loopCount = 0 To LoopTimesintTmp = 255Next loopCounttimeEnd = TimerDebug.Print “Integer :”; timeEnd – timeBegin – timeAddition; “秒”timeBegin = TimerFor loopCount = 0 To LoopTimeslngTmp = 255Next loopCounttimeEnd = TimerDebug.Print “Long :”; timeEnd – timeBegin – timeAddition; “秒”timeBegin = TimerFor loopCount = 0 To LoopTimescurTmp = 255Next loopCounttimeEnd = TimerDebug.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 = 10000000Public Sub test()Dim bytTmp As ByteDim intTmp As IntegerDim lngTmp As LongDim curTmp As CurrencyDim strTmp As StringDim loopCount As LongDim timeBegin As SingleDim timeEnd As SingleDim timeAddition As SingletimeBegin = TimerFor loopCount = 0 To LoopTimesstrTmp = 255Next loopCounttimeEnd = TimertimeAddition = timeEnd – timeBegintimeBegin = TimerFor loopCount = 0 To LoopTimesstrTmp = bytTmp Or 255Next loopCounttimeEnd = TimerDebug.Print “Byte :”; timeEnd – timeBegin – timeAddition; “秒”timeBegin = TimerFor loopCount = 0 To LoopTimesstrTmp = intTmp Or 255Next loopCounttimeEnd = TimerDebug.Print “Integer :”; timeEnd – timeBegin – timeAddition; “秒”timeBegin = TimerFor loopCount = 0 To LoopTimesstrTmp = lngTmp Or 255Next loopCounttimeEnd = TimerDebug.Print “Long :”; timeEnd – timeBegin – timeAddition; “秒”timeBegin = TimerFor loopCount = 0 To LoopTimesstrTmp = curTmp Or 255Next loopCounttimeEnd = TimerDebug.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在位操作上更快。现在你知道该如何选择整数数据类型了吗?微笑拥抱每一天,做像向日葵般温暖的女子。

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

相关文章:

你感兴趣的文章:

标签云: