System.currentTimeMillis() 是 Java 中时间性能的最佳衡量标准吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1073325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is System.currentTimeMillis() the best measure of time performance in Java?
提问by dreadwail
Is System.currentTimeMillis() the best measure of time performance in Java? Are there any gotcha's when using this to compare the time before action is taken to the time after the action is taken? Is there a better alternative?
System.currentTimeMillis() 是 Java 中时间性能的最佳衡量标准吗?使用它来比较采取行动之前的时间和采取行动之后的时间时,是否有任何问题?有更好的选择吗?
采纳答案by David Johnstone
I hope not - it's what I use when i don't use nanoTime()
.
我希望不会 - 这是我不使用时使用的nanoTime()
。
回答by raupach
Before Java 1.5 there was only System.currentTimeMillis. However the granularity of the value depends on the underlying operating system and may be large. On Windows XP I sometimes ended up with gaps of 20ms. I heard Linux is way better with gaps in the range of 1-2ms.
在 Java 1.5 之前,只有 System.currentTimeMillis。但是,该值的粒度取决于底层操作系统并且可能很大。在 Windows XP 上,我有时会以 20 毫秒的间隔结束。我听说 Linux 更好,间隔在 1-2 毫秒之间。
With Java 1.5 you can also use System.nanoTime. I never had problems with this one.
在 Java 1.5 中,您还可以使用 System.nanoTime。我从来没有遇到过这个问题。
回答by karim79
回答by jwoolard
One gotcha is that this measures real time elapsed, not CPU time - so it is very dependent on system load. This is fine if your on an otherwise free machine, but can sometimes produce interesting results if other processes are trying to do work.
一个问题是,它测量的是实时经过的时间,而不是 CPU 时间——因此它非常依赖于系统负载。如果您使用其他免费的机器,这很好,但如果其他进程正在尝试工作,有时会产生有趣的结果。
This articlehas an intersting solution to the problem.
这篇文章有一个有趣的解决方案。
回答by dfa
besides System.nanoTime()
, JMX is probably the best viable option:
此外System.nanoTime()
,JMX 可能是最好的可行选择:
java.lang.management.ManagementFactory.getThreadMXBean()
you can query current thread cpu time (measured in nano seconds but not with nano seconds precision, as for System.nanoTime
())
您可以查询当前线程的 CPU 时间(以纳秒为单位,但不是纳秒精度,如System.nanoTime
())
回答by KarlU
If you need monotonic time measurements, System.nanoTime is a better choice. System.currentTimeMillis is subject to UTC timing variations -- leap seconds, NTP updates and jitter, as well as users setting the system clock*. This can cause some spectacular failures in certain kinds of timing applications. System.nanoTime is supposed to be immune to all that.
如果您需要单调时间测量,System.nanoTime 是更好的选择。System.currentTimeMillis 受 UTC 时间变化的影响——闰秒、NTP 更新和抖动,以及用户设置系统时钟*。这可能会在某些类型的计时应用中导致一些严重的故障。System.nanoTime 应该不受所有这些影响。
The problems with System.nanoTime include periodic numeric overflow and long term timing inaccuracy, making System.currentTimeMillis better for longer time spans (provided that users leave the system clock alone). Note that daylight saving time and time zone changes should not affect System.currentTimeMillis.
System.nanoTime 的问题包括周期性数字溢出和长期计时不准确,这使得 System.currentTimeMillis 更适合更长的时间跨度(前提是用户不使用系统时钟)。请注意,夏令时和时区更改不应影响 System.currentTimeMillis。
*Windows "Sync with internet time" makes stepwise changes. This can be very disruptive, as opposed to a proper NTP client implementation that "chases" the server by adjusting the client timebase frequency.
*Windows“与互联网时间同步”进行逐步更改。这可能是非常具有破坏性的,这与通过调整客户端时基频率“追逐”服务器的正确 NTP 客户端实现相反。
回答by Sonson123
You may use Stopwatch from Google Guavawhich makes measuring time super-easy.
您可以使用Google Guava 的秒表,这使得测量时间变得非常容易。