Question

how do I get out the time my VBA script takes 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?

Was it helpful?

Solution

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.

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top