开发者

VBA in Word: Getting the script execution time

开发者 https://www.devze.com 2023-01-26 00:28 出处:网络
how do I get out the time my VBA script t开发者_运维问答akes to execute? I know from PHP that there is something like microtime() which is called once before the script and once after in order to be a

how do I get out the time my VBA script t开发者_运维问答akes to execute? I know from PHP that there is something like microtime() which is called once before the script and once after in order to be able to calculate the difference from this values...

Is there an VBA equivalent?


It's a sample code that I used in one of my VBA projects to measure the performance of my script temporarily.

Also you can find ample of resources to optimize your script's performance

Public Sub generate(ByRef generators() As Generator)

    Dim startTime As Double

    OptimizePerformance doc

    '/////////////////below is the line that matters    
    startTime = Timer

    '////////// your code that is to be measured (in time) here //////////

    MsgBox Format(Timer - startTime, "00.00") & " seconds"

    removeOptimization doc
End Sub


There's a function called Timer() that returns time in seconds since midnight. Includes milliseconds. I don't know of a micro-second resolution timer in VBA.

An article on About.com suggests that it's possible to write your own microtimer by making Win32 API calls directly from VBA.


In case you need more accurate timing information, I would recommend using one of the following functions the get the start and end times

#If Win64 Then
Private Declare PtrSafe Function GetTickCount Lib "kernel32" () As LongLong
Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As LongLong
#Else
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
#End If

They should return more precise information, depending on systems. Personally, on Win7 64bit / Office 2010/2013 32bit environments I prefer timeGetTime

Note that the absolute values of timeGetTime are not recommended to be used, but the DIFFERENCE (e.g. endTime-startTime) is a quite accurate value in miliseconds

0

精彩评论

暂无评论...
验证码 换一张
取 消