可以在生产代码中使用秒表吗? [英] Can Stopwatch be used in production code?

查看:26
本文介绍了可以在生产代码中使用秒表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个准确的计时器,而 DateTime.Now 似乎不够准确.从我读到的描述来看, System.Diagnostics.Stopwatch 似乎正是我想要的.

I need an accurate timer, and DateTime.Now seems not accurate enough. From the descriptions I read, System.Diagnostics.Stopwatch seems to be exactly what I want.

但我有恐惧症.我对在实际生产代码中使用 System.Diagnostics 中的任何内容感到紧张.(我广泛使用它来调试 Asserts 和 PrintLns 等,但从未用于生产产品.)我不仅仅是尝试使用计时器来对我的功能进行基准测试 - 我的应用程序需要一个实际的计时器.我在另一个论坛上读到 System.Diagnostics.StopWatch 仅用于基准测试,不应在零售代码中使用,尽管没有给出任何理由.这是正确的,还是我(以及发布该建议的人)对 System.Diagnostics 过于封闭?即,可以在生产代码中使用 System.Diagnostics.Stopwatch 吗?谢谢阿德里安

But I have a phobia. I'm nervous about using anything from System.Diagnostics in actual production code. (I use it extensively for debugging with Asserts and PrintLns etc, but never yet for production stuff.) I'm not merely trying to use a timer to benchmark my functions - my app needs an actual timer. I've read on another forum that System.Diagnostics.StopWatch is only for benchmarking, and shouldn't be used in retail code, though there was no reason given. Is this correct, or am I (and whoever posted that advice) being too closed minded about System.Diagnostics? ie, is it ok to use System.Diagnostics.Stopwatch in production code? Thanks Adrian

推荐答案

在幕后,几乎秒表所做的一切就是包装 QueryPerformanceCounter.据我了解,秒表可以提供对高分辨率计时器的访问 - 如果您在生产代码中需要此分辨率,我认为使用它没有任何问题.

Under the hood, pretty much all Stopwatch does is wrap QueryPerformanceCounter. As I understand it, Stopwatch is there to provide access to the high-resolution timer - if you need this resolution in production code I don't see anything wrong with using it.

这篇关于可以在生产代码中使用秒表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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