如果我只想快速测量一个特定函数所用的时间,我可以调用什么来获得准确的时间?鉴于VB6计时函数的精度不高,您是否可以调用Windows API函数呢?
您还可以通过哪些其他方式来衡量应用程序性能?你有没有推荐的第三方工具?
发布于 2009-04-20 02:09:42
我通常使用Windows hihg分辨率性能计数器。查看QueryPerformanceCounter和QueryPerfomanceFrequency
通常,我有一个简单的类,它的构造函数和析构函数调用QueryPerformanceCounter,然后将差值添加到运行总数中。
有关工具,请查看devpartner。虽然它工作得很好,但插装很大一部分代码会使我的应用程序运行慢得让人无法忍受。我通常发现我希望在一两个函数上获得精确的时间,所以我经常使用性能计数器函数,而不是使用devpartner。
发布于 2009-04-20 15:14:58
我使用高性能的多媒体定时器。下面是调试性能分析库的一个片段。
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
Private Declare Function timeEndPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
Private mlTimeStarted As Long
Public Sub StartTimer(Optional lPeriod As Long = 1)
10 Call timeBeginPeriod(lPeriod)
20 mlTimeStarted = timeGetTime()
End Sub
Public Function GetTimeElapsed() As Long
10 GetTimeElapsed = timeGetTime() - mlTimeStarted
End Function
Public Sub EndTimer(Optional lPeriod As Long = 1)
Debug.Assert lPeriod < 10
10 Call timeEndPeriod(lPeriod)
20 mlTimeStarted = 0
End Sub
Public Sub DebugProfileStop()
10 Call EndTimer
End Sub
Public Sub DebugProfileReset()
10 If mlTimeStarted > 0 Then
20 EndTimer
30 End If
40 Call StartTimer
End Sub
Public Sub DebugProfile(sText As String)
10 Debug.Print "Debug " & sText & " : " & CStr(GetTimeElapsed)
End Sub用法:
DebugProfileReset
DebugProfile("Before Loop")
For index = 0 to 10000
DebugProfile("Before Call To Foo")
Foo
DebugProfile("Before Call To Bar")
Bar
DebugProfile("Before Call To Baz")
Baz
Next index
DebugProfile("After Loop")
DebugProfileStop发布于 2009-04-20 03:38:05
VB Watch是另一个您可能想要考虑的工具。
当您可以隔离代码中的可疑区域时,这些功能是最通用的。许多此类型的工具允许您将代码检测的覆盖范围限制为模块或单个过程,或者将监视限制为过程级别而不是语句级别。这可以帮助减少与整个程序的逐行检测相关的一些痛苦。
https://stackoverflow.com/questions/766596
复制相似问题