如何测量 .NET 中多个并发线程的代码块(线程)执行时间
现在我们只使用类似这样的方法
stopWatch.Start();
try
{
method();
}
finally
{
stopWatch.Stop();
}
,这对于同步方法来说效果很好,但有些方法是异步执行的,因此当多个线程执行时,时间会出现偏差。 是否有与 System.Diagnostics.Stopwatch 等效的方法,它仅测量当前线程中花费的时间?
我们希望在内部测试版(alpha?)版本中收集较长时间的数据,并且在分析器下全职运行并不是一个可行的选择。
编辑:为了澄清,我们只想测量执行 method() 所花费的时间,因此如果 Method1() 和 Method2() 同时开始,并且 Method1 在 2 秒标记处完成,Method2 在 4 秒标记处完成,我想要一些东西告诉我 Method1 花费了大约 1 秒执行而 Method2 花费了大约 3 秒执行(假设在前 2 秒内他们平等地共享(假设单核)处理器。
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?
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嗯,来自 Jon Skeet 对这个问题的回答:
在线程应用程序中准确计时一行代码,C#
还有这篇文章:
http://lyon-smith.org/blogs/code-o-rama/archive/2007/07 /17/timing-code-on-windows-with-the-rdtsc-instruction.aspx
似乎没有简单的方法可以做到这一点。
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.
秒表应该测量仅在一个线程中花费的时间。 您需要在每个线程上运行一个单独的实例。 这可能会给你最接近你想要的结果。
或者(如果您想监视整个应用程序而不仅仅是某些方法,这是首选选项),您可以使用各种开箱即用的性能计数器(它们由 .Net 运行时更新)。 网上有很多资料,可以从msdn开始:
http://msdn.microsoft.com/en-us /library/w8f5kw2e(VS.71).aspx
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
我曾经通过为 StopWatch 创建一个包装器来解决类似的问题,该包装器将 StopWatch 实例标记为 ThreadStatic 。
或者,您可以购买 Ants Profiler 等工具来帮助您。
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.
这是一个很酷的工具:
http://technet.microsoft.com/en-us/sysinternals/bb896647。 ASPX
That's a cool tool:
http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx