2

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?

4

3 回答 3

5

这是我在我的一个 VBA 项目中用于临时测量脚本性能的示例代码。

您还可以找到充足的资源来优化脚本的性能

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
于 2011-07-13T23:51:46.633 回答
3

有一个函数调用Timer()它返回自午夜以来的秒数。包括毫秒。我不知道 VBA 中的微秒分辨率计时器。

About.com 上的一篇文章建议可以通过直接从 VBA 进行 Win32 API 调用来编写自己的微计时器。

于 2010-11-18T20:26:41.460 回答
0

如果您需要更准确的时间信息,我建议您使用以下函数之一来获取开始和结束时间

#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

他们应该返回更精确的信息,具体取决于系统。就个人而言,在 Win7 64bit / Office 2010/2013 32bit 环境我更喜欢timeGetTime

timeGetTime请注意,不建议使用绝对值,但 DIFFERENCE(例如 endTime-startTime)是一个非常准确的值(以毫秒为单位)

于 2016-03-30T13:38:19.947 回答