我应该在方法结束时停止秒表吗? [英] Should I Stop Stopwatch at the end of the method?

查看:77
本文介绍了我应该在方法结束时停止秒表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们使用 Stopwatch

public void DoWork()
{
    var timer = Stopwatch.StartNew();
    // some hard work
    Logger.Log("Time elapsed: {0}", timer.Elapsed);
    timer.Stop(); // Do I need to call this?
}

根据MSDN:

在典型的秒表方案中,先调用Start方法,然后然后最终调用Stop方法,然后使用Elapsed属性检查经过时间.

In a typical Stopwatch scenario, you call the Start method, then eventually call the Stop method, and then you check elapsed time using the Elapsed property.

当我对计时器实例不再感兴趣时,我不确定是否应该调用此方法.我应该使用 Stop 方法清除"吗?

I'm not sure if I should call this method when I'm no longer interested with timer instance. Should I "clear up" using Stop method?

编辑

请记住,Logger.Log(..)不会花费任何费用,因为之前会读取 timer.Elapsed .

Keep in mind that Logger.Log(..) costs nothing because timer.Elapsed is read before the logger logs.

推荐答案

否,您无需停止它. Stop() 只是停止跟踪经过的时间.它不会释放任何资源.

No, you don't need to stop it. Stop() just stops tracking elapsed time. It does not free up any resources.

这篇关于我应该在方法结束时停止秒表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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