Calculating Elapsed Time


long start = System.currentTimeMillis(); // do some other stuff... long end = System.currentTimeMillis(); long elapsedTime = end  start;



By calculating elapsed time, we can determine how long it takes to do something or how long a process takes to complete. To do this, we use the System.currentTimeMillis() method to obtain the current time in milliseconds. We use this method at the start and end of the task we want to get the elapsed time for, and then take the difference in times. The actual value that is returned by the System.currentTimeMillis() method is the time since Janauary 1, 00:00:00, 1970 in milliseconds.

JDK 1.5 adds a nanoTime() method to the System class, which allows you to get even more precise timing, down to nanoseconds. In reality, not all platforms support nanosecond resolution, so although the nanoTime() method may be available, you can't always count on getting nanosecond resolution.

This is often useful for testing, profiling, and performance monitoring.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net