.NET 秒表 - 性能损失
可能的重复:
DateTime.Now 是测量时间的最佳方法吗?函数的性能?
秒表与使用 System.DateTime.Now 进行计时事件
我有需要尽可能快运行的代码。为了能够记录执行时间,我使用 秒表 类。我怀疑,秒表可能会对性能产生不良影响。也许使用日期时间差异可能更有效?
您认为哪一款性能更好?
Stopwatch sw = new Stopwatch();
sw.Start();
int a = 5;
// Critical lines of code
long elapsedMs = se.Elapsed.TotalMilliseconds;
或者
DateTime startDate = DateTime.Now;
int a = 5;
// Critical lines of code
long elapsedMs = DateTime.Now.Subtract(startDate).TotalMilleseconds;
Possible Duplicates:
Is DateTime.Now the best way to measure a function's performance?
Stopwatch vs. using System.DateTime.Now for timing events
I have code which needs to run as fast as possible. To be able to log the execution time, I use the Stopwatch class. I suspect, Stopwatch may effect the performance in a bad way. Maybe using a DateTime difference may be more effective?
Which one do you think has better performance?
Stopwatch sw = new Stopwatch();
sw.Start();
int a = 5;
// Critical lines of code
long elapsedMs = se.Elapsed.TotalMilliseconds;
OR
DateTime startDate = DateTime.Now;
int a = 5;
// Critical lines of code
long elapsedMs = DateTime.Now.Subtract(startDate).TotalMilleseconds;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Stopwatch
在调用Start
和Stop
之间没有执行任何操作...它只是存储当前时间戳(通过 QueryPerformanceCounter ) 启动它时将其与停止时的当前时间戳进行比较。因此,它没有理由影响代码的性能,至少不会显着影响。秒表
专为精确的时间测量而设计,因此您可以确保它经过彻底优化。它也比比较DateTime.Now
的连续值要准确得多。 ...The
Stopwatch
isn't doing anything between the calls toStart
andStop
... It just stores the current timestamp (viaQueryPerformanceCounter
) when you start it, and compare it to the current timestamp when you stop it. So there is no reason it could affect the performance of your code, at least not significantly.Stopwatch
was designed specifically for accurate time measurements, so you can be sure it is thoroughly optimized. It is also much more accurate than comparing successive values ofDateTime.Now
. ...由于您的分析代码仅执行一次,因此其性能影响应该可以忽略不计。如果您将对秒表的调用放在内部循环/关键代码路径中,这只是一个问题。
GetTickCount()
应该是最快的分析方法之一,但它的精度只有几毫秒。GetTickCount()
Windows API 函数仅检查一个简单变量(每隔几毫秒更新一次);它的成本只是本机方法调用的成本,仅此而已。它在 .NET 中公开为Environment.TickCount
。但正如我所说,我怀疑这是否重要。DateTime.UtcNow/Now
与GetTickCount
具有相同(低)的精度。理论上,可能会对抖动产生一些影响,但这不太可能。
Since your profiling code gets executed only once, its performance impact should be negligible. It's only a problem if you put calls to stopwatch inside your inner loop/critical codepaths.
GetTickCount()
should be one of the fastest ways to profile, but it only has an accuracy of a few milliseconds. TheGetTickCount()
Windows API function does only check a simple variable (which is updated every few milliseconds); its cost is the cost of a native method invocation and nothing more. It's exposed asEnvironment.TickCount
in .NET. But as I said, I doubt this matters.DateTime.UtcNow/Now
have the same (low) accuracy asGetTickCount
.In theory, there could be some effect on the jitter, but that's unlikely.
答案实际上取决于您想要达到的精度。对于大于秒的精度,秒表是更好的方法,因为它使用比日期时间更精确的测量系统。
请参阅 http://msdn.microsoft.com/en-us/ Library/system.diagnostics.stopwatch.aspx
从性能的角度来看,我怀疑 Start() 和 DateTime.Now 如何存储来自相应测量系统的值以及检索时是否存在很大差异这毫秒 它计算差异并转换(根据需要)到相应的测量单位
The answer really depends on what precision are you trying to achieve. For precision greater than seconds the stopwatch is a better approach due to using a more precise measurement system than the datetime.
See http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx
From the performance point of view, I doubt there is that much of a difference seeing how the Start() and DateTime.Now store the values from the corresponding measurement system and when retrieving the milliseconds it calculates the difference and converts (as required) to the corresponding measurement unit
我认为,如果您只调用几次,这并不重要,但是,一切都取决于您要求的准确度是多少;
Stopwatch
更加准确,因为它依赖于QueuePerformanceCounter
API,因此它使用更高的分辨率。I don't think that it is really matters if you are only calling it a few times, However, everything is dependent on what is the level of accuracy you are asking for;
Stopwatch
is much more accurate, because it relies onQueuePerformanceCounter
API, so it uses a higher resolution.我认为就性能而言,第二个会更有效,正如评论中的链接所示,如果您想测量低于秒的时间,那么 DateTime 将不准确,尽管它会很有效,
我认为是这样,因为 StopWatch与 DateTime 相比,DateTime 会连续测量刻度,DateTime 只保存一个 DateTime 实例,即 startTime。
I think the second one will be more efficient as far as performance is concerned, and as the links in the comment indicate, if you want to measure time below seconds then DateTime won't be accurate although it would be efficient
I think so because StopWatch would be measuring the ticks continuously as compared to DateTime which will only hold one instance of DateTime, that is, startTime.