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)
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)
java.lang.System.nanoTime()
Or you can use the StopWatch that is supplied in apache commons. This class uses java.lang.System.currentTimeMillis()
http://commons.apache.org/lang/api-release/org/apache/commons/lang/time/StopWatch.html
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().
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());
This produces:
StopWatch 'My Stop Watch': running time (millis) = 10000 ----------------------------------------- ms % Task name ----------------------------------------- 02000 020% initializing 05000 050% processing 03000 030% finalizing
StopWatch classes went out with the indians and are generally only used today by captain caveman types, ;-).
You should think in terms of resources and meters (multiples).
http://www.jinspired.com/products/jxinsight/meteringthecloud.html
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.