VBA, MS Access MS Access в примерах

Разница во времени с точностью до милисекунд (API)

По материалам: https://www.aeternusconsulting.com/best-millisecond-timer-vba/

#If VBA7 And Win64 Then ' Performance counter API's
    'for 64-bit Excel
    Declare PtrSafe Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
    Declare PtrSafe Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
#Else
    'for 32-bit
    Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
    Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
#End If

'***********************************************************
'Purpose: Measure elapsed time in milliseconds
'***********************************************************
Sub MilliSecondTimer_Start()
    Dim lgResult As Long
    lgResult = QueryPerformanceCounter(curStartCounter)
End Sub

'***********************************************************
'Purpose: Measure elapsed time in milliseconds
'***********************************************************
Sub MilliSecondTimer_End()
    Dim lgResult As Long
    Dim curFrequency As Currency
    lgResult = QueryPerformanceCounter(curEndCounter)
    lgResult = QueryPerformanceFrequency(curFrequency)
    Debug.Print "Elapsed time (ms): " & (curEndCounter - curStartCounter) / curFrequency
End Sub


How to use the Excel VBA codes in your projects

I think it will benefit you further if I explained how to use the VBA codes in your own projects. I’m assuming you are an intermediate VBA coder and already have the codes copied and placed in a VBA code module.

'***********************************************************
'Purpose: Sample code to measure elapsed time in milliseconds
'***********************************************************
Sub TimeACode()
    'measure start count
    Call MilliSecondTimer_Start
    '*****************************************
    'Insert the code to measure elapsed time
    '*****************************************
    Dim lgCounter As Long
    For lgCounter = 1 To 100000000
        'do nothing
    Next
    'measure end count
    Call MilliSecondTimer_End
End Sub


You can start the millisecond timer with the line:

    Call MilliSecondTimer_Start


This subroutine will store current value of the high-resolution performance counter in the module variable curStartCounter. From that point onward, you can write any VBA code that you wish to measure the elapsed time.

The milliseconds timer can be stopped with the line:

    Call MilliSecondTimer_End
Назад ToTop
L.E. 23.06.2023
Рейтинг@Mail.ru