Java的秒表类 [英] Stopwatch class for Java
本文介绍了Java的秒表类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您应该使用哪个Java类进行时间性能测量?
Which Java class should you use for time performance measurements?
(可以使用任何日期/时间类,但我问的原因是.Net为此目的,有一个指定的秒表课程)
(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)
推荐答案
Spring Framework有一个很好的 StopWatch
class :
The Spring Framework has an excellent StopWatch
class:
StopWatch stopWatch = new StopWatch("My Stop Watch");
stopWatch.start("initializing");
Thread.sleep(2000); // simulated work
stopWatch.stop();
stopWatch.start("processing");
Thread.sleep(5000); // simulated work
stopWatch.stop();
stopWatch.start("finalizing");
Thread.sleep(3000); // simulated work
stopWatch.stop();
System.out.println(stopWatch.prettyPrint());
这会产生:
StopWatch 'My Stop Watch': running time (millis) = 10000
-----------------------------------------
ms % Task name
-----------------------------------------
02000 020% initializing
05000 050% processing
03000 030% finalizing
这篇关于Java的秒表类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文