码迷,mamicode.com
首页 > 其他好文 > 详细

VB6高精度计时类模块

时间:2015-01-08 00:56:56      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:

创建一个类模块,粘贴如下代码:

'大整数结构体
Private Type LARGE_INTEGER

    LowPart As Long
    HighPart As Long

End Type

'获取时间计数器计数值
Private Declare Function QueryPerformanceCounter _
                Lib "kernel32" (lpPerformanceCount As LARGE_INTEGER) As Long

'获取震动频率:一个大整数
Private Declare Function QueryPerformanceFrequency _
                Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long

'用于将大整数的字节复制到Currency类型,完成转换的工作
Private Declare Sub CopyMemory _
                Lib "kernel32.dll" _
                Alias "RtlMoveMemory" (Destination As Any, _
                                       Source As Any, _
                                       ByVal Length As Long)

'初始时间
Private initTime  As LARGE_INTEGER

'每秒震动次数
Private freqCount As LARGE_INTEGER

'采样时振动器震动计数值
Private logTime   As LARGE_INTEGER

'每秒震动次数的Currency类型数据
Private fFreq     As Currency

'将一个长整型转换成高精度型数据
Private Function LargeIntToCurrency(liInput As LARGE_INTEGER) As Currency
    CopyMemory LargeIntToCurrency, liInput, LenB(liInput)
    LargeIntToCurrency = LargeIntToCurrency * 10000
End Function

'类初始化
Private Sub Class_Initialize()

    '获取振荡器的震荡频率
    If QueryPerformanceFrequency(freqCount) > 0 Then
        If QueryPerformanceCounter(initTime) > 0 Then
        Else
            Err.Raise 11, , "记录震荡次数出错!"
        End If

    Else
        Err.Raise 11, , "读取震荡频率出错!"
    End If

    fFreq = LargeIntToCurrency(freqCount)
End Sub

'重置计数器
Public Sub ReInit()
    Call Class_Initialize
End Sub

'重置计数器,并返回从上次重置计数器开始共流失的秒数。
Public Function ReInitGet() As Currency

    Dim cuStart As Currency

    Dim cuEnd   As Currency

    QueryPerformanceCounter logTime
    cuStart = LargeIntToCurrency(initTime)
    cuEnd = LargeIntToCurrency(logTime)
    ReInitGet = (cuEnd - cuStart) / fFreq
    QueryPerformanceCounter initTime
End Function

'返回从上次重置计数器开始共流失的秒数,但不重置计数器
Public Function SmoothGet() As Currency

    Dim cuStart As Currency

    Dim cuEnd   As Currency

    QueryPerformanceCounter logTime
    cuStart = LargeIntToCurrency(initTime)
    cuEnd = LargeIntToCurrency(logTime)
    SmoothGet = (cuEnd - cuStart) / fFreq
End Function

该类可用于程序执行过程中的代码优化。因为它可以帮你了解某个函数中各个步骤区间的耗时。


VB6高精度计时类模块

标签:

原文地址:http://blog.csdn.net/greatbody/article/details/42508237

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!