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

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

  看到这里,我想大家都应该很清楚了,虽然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 秒
*********************

本文关键:四种整数数据类型的性能对比
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top