Java System.nanoTime () Huge difference in elapsed time

I am in android widgets and checked the elapsed time between two calls to System.nanoTime (), and the number is huge. How do you measure elapsed time with this? it should be the phrase of a second, and instead much more. Thanks

+6
java android nanotime
source share
3 answers

System.nanoTime() returns the value of time, the granularity of which is nanosecond; those. 10 -9 seconds as described in javadoc . The difference between the two System.nanoTime() calls, which make up a significant fraction of a second, is limited to a large number.


If you need a time measure with more granularity, consider System.currentTimeMillis() ... or just divide the nanosecond values ​​by the appropriate power of 10 according to your application.

Please note that on the Android platform there are 3 different system clock cycles that support different β€œmeasures” of time; see SystemClock . If you are programming explicitly for the Android platform, you should read javadoc and decide which measure is most suitable for what you are doing.


For your information, β€œnano-” is one of the standard prefixes defined by the International System of Units (SI) - see http://physics.nist.gov/cuu/Units/prefixes.html .

If you really think that β€œthey” were wrong and that β€œnano” is too small, you can always write a letter to NIST. I'm sure someone will appreciate this ... :-)

+17
source share

One second contains 1,000,000,000 nanoseconds, so as long as your number is in this range, this is reasonable.

+7
source share

If you want it in fractional form, just take value / 10 ^ 9 where value your difference in nanoTime() s.

 long nanoSeconds = 500000000; float seconds = nanoSeconds / 1000000000; Log.i("NanoTime", nanoSeconds + " ns is the same as " + seconds + " seconds"); 

Your result will be:

 07-27 11:35:47.196: INFO/NanoTime(14237): 500000000 ns is the same as 0.5 seconds 
+1
source share

All Articles