开发者

how to set timer for calculate execution time [duplicate]

This question already has answers here: Is DateTime.Now the best way to measure a function's performance? [closed] (16 answers) Closed 9 years ago. 开发者_如何学Python

i like to set timer for calculating execution time in c# for particular process in my execution. how can i do this


You don't generally use a timer for this - you use a Stopwatch.

Stopwatch sw = Stopwatch.StartNew();
// Do work
sw.Stop();
TimeSpan elapsedTime = sw.Elapsed;

If you're performing benchmarking of something relatively fast, it's worth doing it many, many times so that the time taken is significant (I usually go for 5-30 seconds). That's not always feasible, admittedly - and there are many more subtleties which affect real world performance (such as cache hits/misses) which micro-benchmarking often misses out on.

Basically, be careful - but Stopwatch is probably the best starting point in many cases.


You can use System.Diagnostics.Stopwatch to do what you want.


You can use a Timer and attach an action to the Elapsed event. For instance you can abort your thread... hmm if you are not affraid of the consequences... Actually you can do that if you want to set a timeout to your process for instance.

If it's just for monitoring, Stopwatch just fits your needs

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜