4

在我們的VB6應用程序中,我們添加了一些用於跟蹤函數花費時間的實用函數。我們這樣做是爲了跟蹤性能瓶頸。vb.net中函數調用的跟蹤持續時間

基本上,它是如何工作的,有兩個實用函數:StartTickCount()和EndTickCount()。您可以在每個函數中傳遞函數的名稱,函數會在調用StartTickCount()時使用字典來獲取滴答計數,然後在調用EndTickCount()時減去滴答計數。這並不完美,因爲它當然沒有考慮到獲取嘀嘀計數的電話需要時間等,但基本上它適用於我們的目的。在粗端部分的疼痛,確保在每個功能和EndTickCount()在每個出口點開始打電話StartTickCount():

Private Function SomeFuction() as String 
    ' indicate the function started 
    StartTickCount("MyClass.SomeFunction") 


    ' some logic that causes the function to end 
    If (some logic) Then 
     EndTickCount("MyClass.SomeFunction") 
     Return "Hello!" 
    End If 

    ' final exit point 
    EndTickCount("MyClass.SomeFunction") 
    Return "World" 
End Function 

反正是有內置的任何功能,既可以通過Visual Studio 2010調試器或在System.Reflection命名空間中,在VB.NET中做類似的事情?

基本上,我想要的是記錄每個函數被調用的次數,在該函數中花費的時間總量,在該函數中花費的平均秒數以及花費在該函數中的最大時間量該功能中的單個呼叫。

我當然可以手工編寫這(因爲我們已經做了在VB6一次),但如果有現有的工具,使其更容易,我寧願使用它們。

+0

你有沒有考慮使用一個分析器? – 2012-02-15 14:58:36

+0

聽起來像一個分析器可以工作,至少在調試模式下運行。但是我們可能還需要檢查客戶端服務器(不包含VS2010工具)的性能,那麼是否還有任何編碼選項? – 2012-02-15 15:04:15

回答

3

我會看看秒錶類 - 它是專爲這種類型的事情設計的。

但是也有一個夢幻般的工具,在那裏已經做到這一點,它實際上做很少的工作做得更好。它還有一個爲期10天的免費試用版。

http://www.jetbrains.com/profiler/

(我不爲工作,我不隸屬於JetBrains公司 - 但我希望我是因爲他們做一些非常酷的產品)

+1

Stopwatch類的MSDN URL:https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.100%29.aspx。 – 2016-01-20 20:42:53

6

您可以通過修改/調整下面的代碼到你需要獲得相同的結果:

'Create a variable for start time: 
    Dim TimerStart As DateTime 
    TimerStart = Now 
' 
'place your computing here 
' 
    Dim TimeSpent As System.TimeSpan 
    TimeSpent = Now.Subtract(TimerStart) 
    MsgBox(TimeSpent.TotalSeconds & " seconds spent on this task") 

可以查看時間跨度文檔直接獲得時間以毫秒爲單位或以其它格式。

0

我ofently使用這個輔助類。它基本上做什麼@AndreaAntonangeli建議,但安排了一個更乾淨的代碼:

Public Class TimeSpanHelper 

    Dim caller_name As String 
    Dim timer_start As DateTime 
    Dim time_spent As System.TimeSpan 
    Dim prev_time As DateTime 
    Dim time_diff As System.TimeSpan 

    Public Sub New(caller As String) 
     caller_name = caller 
    End Sub 

    Public Sub startClock() 
     timer_start = DateTime.Now 
     Console.WriteLine(caller_name & " starts at:" & timer_start.ToString) 
     prev_time = timer_start 
    End Sub 

    Public Sub getElapsedTime(Optional flag As String = "") 
     time_spent = Now.Subtract(timer_start) 
     time_diff = Now.Subtract(prev_time) 
     prev_time = DateTime.Now 
     Console.WriteLine(caller_name & "-" & flag & " " & time_spent.TotalSeconds & "s from start (" & time_diff.TotalSeconds & "s from previous flag)") 
    End Sub 

    Public Sub stopClock() 
     Dim timer_stop As DateTime = DateTime.Now 
     time_spent = Now.Subtract(timer_start) 
     time_diff = Now.Subtract(prev_time) 
     Console.WriteLine(caller_name & " ends at:" & timer_stop.ToString & " - " & time_spent.TotalSeconds & " seconds elapsed from start (" & time_diff.TotalSeconds & "s from previous flag)") 
    End Sub 

End Class 

使用示例:

'Put this wherever you want to start to count 
Dim timer As TimeSpanHelper = New TimeSpanHelper("MyFunctionName") 
timer.startClock() 
'Do something ... 
timer.getElapsedTime("flag1") 
'Do something ... 
timer.getElapsedTime("flag2") 
'Do something ... 
timer.stopClock() 

輸出應該是這樣的:

MyFunctionName starts at 30/03/2017 16:57:21 
MyFunctionName - flag1 Xs from start (Xs from previous flag) 
MyFunctionName - flag2 Xs from start (Xs from previous flag) 
MyFunctionName ends at 30/03/2017 16:59:14 - 120s elapsed from start (Xs from previous flag)