views:

1423

answers:

5

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)

+2  A: 

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

Kevin Crowell
`nanoTime` has a much higher resolution (at least on Windows): 1-2 microseconds vs. 16 *milli*seconds for `currentTimeMillis`
Daniel Fortunov
A: 

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().

daniel
I'd like to write a simple integration test that makes sure something is 'reasonably performant'.
ripper234
+5  A: 

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
Adam Paynter
A: 

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

William Louth
Hi; you don't list an e-mail, so I can't contact you off-forum - but many of your posts are getting spam flags. There is a fine line between product enthusiasm and astroturfing - you may want to review: http://meta.stackoverflow.com/questions/8323/astroturfing-on-stack-overflow
Marc Gravell
Maybe I should have rephrased my comment to "Have you considered that maybe a measuring just by clock time is not sufficient for the type of real production performance issues that exist. Most systems even no software systems are optimized by analyzing the activities performed and the various resources consumed during such periods."
William Louth
Does the world need another stopwatch class? NO. Are they effective? NO. Why? Because it assumes that person instrumenting the code already know were the bottlenecks are which we know to be a falsehood because developers are so divorced from the production systems and real-world workloads.I will probably get marked down for this but here is how to do this for anything other than a helloworld application.http://williamlouth.wordpress.com/2009/04/06/instrument-measure-refine-repeat/
William Louth
Mike Dunlavey
A: 

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.

Mike Dunlavey