Java 的秒表类
您应该使用哪个 Java 类来测量时间性能?
(可以使用任何日期/时间类,但我问的原因是在 .Net 中有一个指定的 Stopwatch 类用于此目的)
Which Java class should you use for time performance measurements?
(One could use any date/time class, but the reason I'm asking is in .Net there's a designated Stopwatch class for this purpose)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Spring框架有一个优秀的
StopWatch
类:这会产生:
The Spring Framework has an excellent
StopWatch
class:This produces:
这是如何使用 StopWatch 通过注释专用方法来设置多个测量值的示例。 非常有用且非常易于使用来测量例如多个嵌入式调用/操作上的服务调用等。
StopWatchHierarchy
Advisor
ProfileExecution Annotation
注释您的代码
输出
This is an example how to use StopWatch to set multiple measurements by annotating dedicated methods. Very useful and very simple to use to measure e.g. Service call over multiple embedded call / operation and so on.
StopWatchHierarchy
Advisor
ProfileExecution Annotation
Annotate your code
Output
您可以尝试 System.currentTimeMillis(),而且在一些众所周知的 IDE(例如 eclipse 和 netbeans)下也有很好的分析选项。 此外,除了 IDE,您还可以在性能测量任务中尝试独立分析器。 我认为通过使用探查器,您将获得比使用更好的结果 System.currentTimeMillis()。
You can try System.currentTimeMillis(), but also there a good profiling options under some well known IDEs, such as eclipse and netbeans. Also, away from the IDE, you can try standalone profilers in your performance measurements tasks. I think that by using profilers you will get better results than using System.currentTimeMillis().
如果您只想测量它,请使用秒表类,或者只是一个秒表。
如果您想让速度更快,考虑这个。
If you just want to measure it, use a stopwatch class, or maybe just a stopwatch.
If you want to make it faster, consider this.
最好是使用 System.nanoTime(),但是,如果您想像 System.Diagnostics.Stopwatch 一样获得 Ticks(经过的 Ticks),那么您需要将纳秒转换为 Ticks(1 Tick = 100 纳秒),然后开始在之间转换纳秒和毫秒、秒、分钟、小时,然后最后将输出格式化为时间表示形式,例如 Elapsed() 方法之一 (hh:mm:ss.sssssss),但是,看起来 Java 中的日期仅使用 3 毫秒(hh:mm:ss.sss),所以你还需要练习格式。
我为 Java 做了一个秒表课程
您可以从以下位置获取它: http://carlosqt.blogspot.com /2011/05/stopwatch-class-for-java.html
示例:
输出:
希望这会有所帮助。
The best is to use System.nanoTime(), however, if you want to get Ticks (elapsed Ticks) like System.Diagnostics.Stopwatch does you then need to convert nanoseconds to Ticks (1 Tick = 100 Nanoseconds) and then start converting between nanos and millis, secs, mins, hours, then finally format the output into a Time representation such as the one of the Elapsed() method (hh:mm:ss.sssssss), however, looks like Dates in Java use only 3 milliseconds (hh:mm:ss.sss), so you also need to workout the format as well.
I did one Stopwatch class for Java
you can get it from: http://carlosqt.blogspot.com/2011/05/stopwatch-class-for-java.html
Example:
The output:
Hope this helps.
System. nanoTime()
StopWatch
在 阿帕奇Commons Lang 库。
System.nanoTime()
StopWatch
that is supplied in the ApacheCommons Lang library.
查看 perf4j。 Spring的秒表主要是为了本地开发。 Perf4j 可以支持您的 POC 类型计时以及生产环境。
Check out perf4j. Spring's stop watch is mainly for local development. Perf4j can support both your POC type timing as well as on a production environment.
Performmetrics 提供了方便的秒表类。 它可以测量挂钟时间等:如果您需要更高的精度,它还可以测量 CPU 时间、用户时间和系统时间。
它很小,免费,您可以从 Maven Central 下载。
更多信息和示例可以在这里找到: https://obvj.net/performetrics
这会产生类似于以下内容的输出以下内容:
您只需传递自定义参数即可将指标转换为任何时间单位(纳秒、毫秒、秒等)。
PS:我是该工具的作者。
Performetrics provides a convenient Stopwatch class. It can measure wall-clock time and more: it also measures CPU time, user time and system time, if you need more accuracy.
It's small, free and you can download from Maven Central.
More information and examples can be found here: https://obvj.net/performetrics
This produces an output similar to the following:
You can convert the metrics to any time unit (nanoseconds, milliseconds, seconds, etc...) just by passing a custom parameter.
PS: I am the author of the tool.
如果您使用 JDK 9+,则可以使用 Flight Recorder。 它具有极低的开销,并使用不变的 TSC 进行计时,这比 System.nanoTime() 的侵入性更小。
您还可以从命令行(-XX:StartFlightRecording)开始记录,然后在配置文件(.jfc)中启用该事件(如果默认禁用它,@Enable(false))
这样,JIT 通常会优化 StopWatch 调用(转义分析、内联、死代码消除等),因此只有在您想要测量某些内容时才需要付出代价。
If you are using JDK 9+, you could use Flight Recorder. It has extremely low overhead and uses invariant TSC for timing, which is less intrusive than System.nanoTime()
You can also start a recording from command line (-XX:StartFlightRecording) and then enable the event in a configuration file (.jfc) (if you disable it by default, @Enable(false))
That way the JIT will typically optimize away the StopWatch calls (escape analysis, inlining, deadcode elimination etc), so you only pay the penalty when you want to measure something.