Java Precise MeasurementCodeRunning time:
Long starttime = system. nanotime (); // start time
For (INT I = 0; I <10000; I ++ ){
;
}
Long consumingtime = system. nanotime ()-starttime; // time consumed
System. Out. println (consumingtime );
System. Out. println (consumingtime/1000 + "microsecond ");
JDK explanation:
Public static longNanotime()
Returns the current value of the most precise available system timer, in nanoseconds.
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. the value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative ). this method provides nanosecond precision, but not necessarily nanosecond accuracy. no guarantees are made about how frequently values change. differences in successive cballs that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.
For example, to measure how long some code takes to execute:
Long starttime = system. nanotime (); //... the code being measured... long estimatedtime = system. nanotime ()-starttime;
Return Value:
The current value of the system timer, in nanoseconds.
Since:
1.5