跟踪 vb.net 中函数调用的持续时间

发布于 2025-01-05 20:29:19 字数 945 浏览 1 评论 0原文

在我们的 VB6 应用程序中,我们添加了一些实用函数来跟踪函数所花费的时间。我们这样做是为了跟踪性能瓶颈。

基本上,它的工作原理是有两个实用函数:StartTickCount() 和 EndTickCount()。您将在每个函数中传递函数名称,函数将使用字典在调用 StartTickCount() 时获取滴答计数,然后在调用 EndTickCount() 时减去滴答计数。这并不完美,因为它当然没有考虑到调用获取滴答计数需要时间等,但基本上它符合我们的目的。屁股部分的痛苦在于确保在每个函数的开头调用 StartTickCount() 并在每个退出点调用 EndTickCount():

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

无论如何,是否有任何内置功能,无论是通过 VS 2010 调试器还是在 中System.Reflection 命名空间,在 VB.NET 中执行类似的操作?

基本上,我想要的是记录每个函数被调用的次数、该函数花费的总时间、该函数花费的平均秒数以及该函数中单次调用花费的最大时间。功能。

我当然可以手工编写(因为我们已经在 VB6 中编写过一次),但如果有现有的工具可以使其更容易,我宁愿使用它们。

In our VB6 applications, we added a few utility functions for tracking the amount of time spent in a function. We did this to track performance bottlenecks.

Basically, how it worked was there were two utility functions: StartTickCount() and EndTickCount(). You would pass the name of the function in each, and the functions would use a dictionary to get the tick count when StartTickCount() was called, and then subtract the tick count when EndTickCount() was called. This wasn't perfect because it didn't of course take into consideration that calls to get tick count takes time, etc, but basically it worked for our purposes. The pain in the butt part was making sure to call StartTickCount() at the beginning of each function and EndTickCount() at each exit point:

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

Anyway, is there any functionality built in, either through the VS 2010 debugger or in the System.Reflection namespace, to do something similar in VB.NET?

Basically, what I want is to log the number of times each function is called, the total amount to time spent in that function, the average number of seconds spent in that function, and the maximum amount of time spent in a single call in that function.

I can certainly write this by hand (since we already did once in VB6), but if there are existing tools to make it easier, I'd rather use them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拿命拼未来 2025-01-12 20:29:19

您可以通过根据您的需要修改/调整以下代码来实现相同的结果:

  '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")

您可以查看有关 TimeSpan 的文档以直接获取以毫秒或其他格式表示的时间。

You can achieve the same results by modifying/adapting the following code to your needs:

  '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")

You can check documentation about TimeSpan to get directly time in milliseconds or in other formats.

反差帅 2025-01-12 20:29:19

我想看看秒表类 - 它是为此类事情设计的。

然而,已经有一个很棒的工具可以做到这一点,而且它实际上只需很少的工作就可以做得更好。它还有 10 天的免费试用期。

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

(我不工作,也不是隶属于jetbrains - 但我希望我是因为他们制造了一些非常酷的产品)

I'd have a look at the stopwatch class - it's designed for this type of thing.

However there is also a fantastic tool out there to do this already and it actually does a better job with very little work. It has a 10-day free trial as well.

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

(I don't work for and am not affiliated with jetbrains - but I wish I was because they make some really cool products)

ま昔日黯然 2025-01-12 20:29:19

听起来您需要在应用程序中添加一些分析。您可能需要从初学者分析指南开始与VS

您还可以在应用程序中添加性能计数器以获取运行时指标。请参阅 http://msdn.microsoft.com/en -us/library/w8f5kw2e(v=vs.100).aspx

有关工具和技术的更完整列表,请参阅 http://msdn.microsoft.com /en-us/magazine/hh288073.aspx

It sounds like you need to add some profiling in your application. You may want to start with the beginner's guide to profiling with VS.

You can also add performance counters in your application for runtime metrics. See http://msdn.microsoft.com/en-us/library/w8f5kw2e(v=vs.100).aspx.

For a fuller list of tools and techniques, see http://msdn.microsoft.com/en-us/magazine/hh288073.aspx.

下雨或天晴 2025-01-12 20:29:19

我经常使用这个辅助类。它基本上按照@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)

I ofently use this helper class. It basically does what @AndreaAntonangeli suggested but arranged to have a cleaner code:

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

Example of use:

'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()

Your output should be something like:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文