Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Word中的VBA:获取脚本执行时间_Vba_Ms Word_Execution Time - Fatal编程技术网

Word中的VBA:获取脚本执行时间

Word中的VBA:获取脚本执行时间,vba,ms-word,execution-time,Vba,Ms Word,Execution Time,如何计算VBA脚本执行所需的时间? 我从PHP中知道,有一个类似于microtime()的东西,它在脚本之前调用一次,在脚本之后调用一次,以便能够计算与此值的差异 是否有VBA等价物?有一个名为Timer()的函数,它从午夜开始以秒为单位返回时间。包括毫秒。我不知道VBA中有微秒分辨率的定时器 建议您可以通过直接从VBA调用Win32 API来编写自己的微计时器。这是我在一个VBA项目中使用的示例代码,用于临时测量脚本的性能 您还可以找到足够的资源来优化脚本的性能 Public Sub gene

如何计算VBA脚本执行所需的时间? 我从PHP中知道,有一个类似于microtime()的东西,它在脚本之前调用一次,在脚本之后调用一次,以便能够计算与此值的差异


是否有VBA等价物?

有一个名为
Timer()
的函数,它从午夜开始以秒为单位返回时间。包括毫秒。我不知道VBA中有微秒分辨率的定时器


建议您可以通过直接从VBA调用Win32 API来编写自己的微计时器。

这是我在一个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

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

#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 64位/Office 2010/2013 32位环境中,我更喜欢时间获取时间


请注意,不建议使用
timeGetTime
的绝对值,但差值(例如endTime startTime)是一个以毫秒为单位的非常精确的值,感谢您的回答,但是否有可能以毫秒为单位获取此值?Timer()返回类似于5406.123的值,其中123表示毫秒。所以,是的,你有一个毫秒计时器。