你如何衡量代码块(线程)的执行时间与.NET多个并发线程 [英] How do you measure code block (thread) execution time with multiple concurrent threads in .NET

查看:218
本文介绍了你如何衡量代码块(线程)的执行时间与.NET多个并发线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我们只需要使用像这样

Right now we just use something like this

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

工作正常进行同步的方法,但也有一些异步执行,所以时间当多个线程在执行歪斜。是否有一个相当于System.Diagnostics.Stopwatch将测量当前线程只花费的时间?

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.

编辑:为了澄清,我们希望只测量所花的时间执行()方法,因此,如果方法1()和method2()都在同一时间启动和方法1结束在2秒钟的标记和方法2完成在4秒大关,我想这会告诉我,方法一用了约1秒执行和方法2花费约3秒钟执行(前2秒内,假设他们共享(假设单核)处理器一样。

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.

推荐答案

嗯,从乔恩斯基特的回答这个问题:

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

http://stackoverflow.com/questions/252793/timing-a-line-of-code-accurately-in-a-threaded-application-c

和还这篇文章:

HTTP://里昂 - 史密斯。组织/博客/代码奥拉玛/存档/ 2007/07/17 /时间码上 - 窗口 - 用最RDTSC-instruction.aspx

好像有没有简单的方法来做到这一点。

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

这篇关于你如何衡量代码块(线程)的执行时间与.NET多个并发线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆