简体   繁体   English

如何使用.NET中的多个并发线程来衡量代码块(线程)执行时间

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

Right now we just use something like this 现在我们只是使用这样的东西

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

Which works fine for synchronous methods, but some are executing asynchronously, so the time is skewed when multiple threads are executing. 哪种方法适用于同步方法,但有些方法是异步执行的,因此在执行多个线程时时间会有所偏差。 Is there an equivalent to System.Diagnostics.Stopwatch that will measure only time spend in the current thread? 是否有相当于System.Diagnostics.Stopwatch,它只会测量当前线程的时间花费?

We want to collect data over an extended period of time in our internal beta(alpha?) release, and running under a profiler full time isn't a feasible option. 我们希望在我们的内部beta(alpha?)版本中长时间收集数据,并且在分析器的全时运行不是一个可行的选择。

Edit: To clarify, we want to only measure the time spent executing method(), so if Method1() and Method2() both start at the same time and Method1 finishes at the 2 second mark and Method2 finishes at the 4 second mark, I want something that would tell me that Method1 spent about 1 second executing and Method2 spend about 3 seconds executing (Assuming during the first 2 seconds they shared the (assumed single core) processor equally. 编辑:为了澄清,我们只想测量执行method()所花费的时间,所以如果Method1()和Method2()都同时启动而Method1在2秒标记处完成而Method2在4秒标记处完成,我想要一些能告诉我Method1花费大约1秒执行而Method2花费大约3秒执行的东西(假设在前2秒内他们平均共享(假设单核)处理器。

Hmm, from Jon Skeet's answer to this question: 嗯,来自Jon Skeet对这个问题的回答:

Timing a line of code accurately in a threaded application, C# 在线程应用程序中准确定位一行代码,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 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. 似乎没有简单的方法可以做到这一点。

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). 或者(如果要监视整个应用程序而不仅仅是某些方法,则首选选项),可以使用开箱即用的各种性能计数器(它们由.Net运行时更新)。 There is lots of information on the web, you can start with msdn: http://msdn.microsoft.com/en-us/library/w8f5kw2e(VS.71).aspx 网上有很多信息,你可以从msdn开始: http//msdn.microsoft.com/en-us/library/w8f5kw2e( VS.71) .aspx

I once solved a similar problem by creating a wrapper for StopWatch that had a StopWatch instance marked as ThreadStatic . 我曾经通过为StopWatch创建一个包装器来解决类似问题,该包装器的StopWatch实例标记为ThreadStatic

Alternatively there are tools such as Ants Profiler you can purchase to help you. 或者,您可以购买Ants Profiler等工具来帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 精确测量线程中代码的执行时间(C#) - Precisely measure execution time of code in thread (C#) 只有当可选的主线程任务和工作线程完成时,我如何保证代码的执行? - How do I guarantee execution of code only if and when optional main thread task and worker threads are finished? 在C#中测量线程的执行时间 - Measure execution time of a thread in C# 如何衡量.NET Core中所有方法的执行时间? - How to measure the execution time of all methods in .NET Core? 重复一段代码来衡量其执行时间的鲁棒性如何? - How robust is repeating a piece of code to measure it's execution time? 如何测量C#中每一行的执行时间 - How do I measure execution time for each row in C# 如何测量 C# 中并行任务或多线程程序的总执行时间? - How to measure total execution time of a parallel task or multi-thread program in c#? 你如何跟踪线程/线程上下文? - How do you keep track of threads/thread contexts? 如何计算.NET应用程序中的并发线程数量? - How to count the amount of concurrent threads in .NET application? 如何在 C# 中运行可变数量的并发参数化无限循环类型的线程? - How do you run a variable number of concurrent parametrizable infinite loop type of threads in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM