How do you measure code block (thread) execution time with multiple concurrent threads in .NET

后端 未结 4 1986
说谎
说谎 2021-01-01 01:13

Right now we just use something like this

        stopWatch.Start();            
        try
        {
            method();
        }
        finally
               


        
相关标签:
4条回答
  • 2021-01-01 01:50

    That's a cool tool: http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx

    0 讨论(0)
  • 2021-01-01 01:54

    The stopwatch should measure time spent only in one thread. You would need to run a separate instance on every thread. This would give you probably the closes results to what you want.

    Alternatively (preferred option if you want to monitor the whole application, and not just some methods), there are various performance counters you can use out of the box (they are updated by .Net runtime). There is lots of information on the web, you can start with msdn: http://msdn.microsoft.com/en-us/library/w8f5kw2e(VS.71).aspx

    0 讨论(0)
  • 2021-01-01 02:04

    I once solved a similar problem by creating a wrapper for StopWatch that had a StopWatch instance marked as ThreadStatic.

    Alternatively there are tools such as Ants Profiler you can purchase to help you.

    0 讨论(0)
  • 2021-01-01 02:12

    Hmm, from Jon Skeet's answer to this question:

    Timing a line of code accurately in a threaded application, C#

    And also this article:

    http://lyon-smith.org/blogs/code-o-rama/archive/2007/07/17/timing-code-on-windows-with-the-rdtsc-instruction.aspx

    It seems like there's no simple way to do this.

    0 讨论(0)
提交回复
热议问题