2009-04-20 55 views
4

如果我只是想快速測量一個特定功能需要多長時間,我可以調用什麼來獲得準確的時間?鑑於VB6定時功能的精確度不高,是否有您調用的Windows API函數?在VB6中定時功能/測量性能的最佳方式是什麼?

您以什麼方式衡量應用程序的性能?您有推薦的第三方工具嗎?

回答

4

我通常使用Windows hihg分辨率性能計數器。退房QueryPerformanceCounterQueryPerfomanceFrequency

通常我有一個簡單的類,其構造函數和析構函數調用QueryPerformanceCounter,然後將差異添加到運行總數。

工具退房devpartner。雖然它運行良好,但重要的代碼部分使我的應用程序運行速度緩慢。我通常會發現我希望在一個或兩個函數上獲得精確的時序,所以我經常最終使用性能計數器函數,而不是使用devpartner。

+0

任何機會,你可以使用QPC發佈VB6代碼?我很好奇你將如何去宣佈和使用LARGE_INTEGER。我不是「VB人」。 – 2009-04-20 04:23:02

+1

我發現這個,很有用:http://support.microsoft.com/kb/172338 – Gavin 2009-04-20 04:37:53

1

VB Watch是您可能需要考慮的另一個工具。

這些東西是最通用的,當你可以隔離你的代碼的可疑區域。許多此類工具允許您將代碼工具的覆蓋範圍限制爲模塊或單個過程,或將監控範圍限制爲過程而非語句級別。這可以幫助減少與整個程序的逐行儀器相關的一些痛苦。

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 
相關問題