在VB6中,定时功能/测量性能的最佳方法是什么?

时间:2009-04-20 02:03:22

标签: performance optimization vb6

如果我只是想快速测量特定功能的持续时间,我可以调用什么来获得准确的时间?鉴于VB6定时函数的精度不高,您调用的是Windows API函数吗?

您还可以通过其他方式衡量应用程序性能?您推荐使用第三方工具吗?

3 个答案:

答案 0 :(得分:4)

我通常使用Windows hihg分辨率性能计数器。查看QueryPerformanceCounterQueryPerfomanceFrequency

通常我有一个简单的类,其构造函数和析构函数调用QueryPerformanceCounter,然后将差值添加到运行总计中。

对于工具,请查看devpartner。虽然它运行良好,但检测大量代码会使我的应用程序运行速度极慢。我通常发现我希望只在一两个函数上获得精确的时序,所以我经常最终使用性能计数器函数而不是使用devpartner。

答案 1 :(得分:4)

我使用高性能多媒体定时器。这是一个调试片段 剖析图书馆。

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

答案 2 :(得分:1)

VB Watch是您可能需要考虑的另一个工具。

当您可以隔离代码的可疑区域时,这些功能最为通用。许多此类工具允许您将代码检测的范围限制为模块或单个过程,或将监视限制到过程而不是语句级别。这有助于减少与整个计划的逐行仪器相关的一些痛苦。