16

I created a filter that monitors the length of a request.

long start = System.nanoTime();

...

long end = System.nanoTime();

How can I get the number of milliseconds from this now?

Blankman
  • 259,732
  • 324
  • 769
  • 1,199

6 Answers6

51
(end - start) / 1000000

1 microsecond = 1000 nanoseconds

1 millisecond = 1000 microseconds

Note, that the result will be rounded down, but you usually don't get true nanosecond accuracy anyway (accuracy depends on the OS). From the Javadoc on nanoTime():

This method provides nanosecond precision, but not necessarily nanosecond accuracy.

Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • 7
    And just in case... Precision roughly means the number of decimals (the number 1.00000000000135738 has a lot of precision) whereas accuracy says something about how many of those decimals are right. For example 1.0001 with an accuracy of 0.01 could be anywhere between 1.0101 and 0.9901, whereas 1.0 with an accuracy of 0.000001 is guaranteed to be somewhere between 0.000009 and 1.000001 – Michael Clerx Sep 06 '10 at 23:53
  • 2
    that works, but the number it returns is like 10, or 5 instead of 10.234234234 or something. do I have to cast? – Blankman Sep 07 '10 at 00:22
  • 1
    @Blankman: If you want to get a number like 10.234234234, you can use `(end - start) / (double) 1000000` (or `(end - start) / 1000000.`, or `(end - start) / (float) 1000000` if you want float) – Chris Lercher Sep 07 '10 at 00:26
  • What about overflow? Does System.nanoTime make the guarantee that it won't overflow? – big lep Mar 21 '13 at 16:50
  • 2
    Because nanoTime uses 64 bit longs, the overflow will only happen when you have successive calls spanning more than 292 years (http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#nanoTime()) – Gerard Oct 09 '13 at 17:04
  • 2
    @MichaelClerx I presume you meant between 0.999999 and 1.000001. – annedroiid Sep 29 '16 at 06:59
12

Also note that you can use the TimeUnit class to help with conversion. With older versions of Java, the following code might be an example to transform a processing time into some other time format:

long startTime = System.nanoTime();

//Processing in-between.

long endTime = System.nanoTime();
long duration = endTime - startTime;
duration = TimeUnit.SECONDS.convert(duration, TimeUnit.NANOSECONDS);

Note that newer versions of Java have shortcuts in the TimeUnit class.

The above sample will turn nanoseconds long into seconds. Also note that this truncates it so you do lose some precision. Therefore, if you switch to minutes then you will lose the precision of seconds. If you want to get a result of "12 minutes and 32 seconds" then you would have to do further processing with this solution.

BoBoCoding
  • 161
  • 1
  • 4
12

TimeUnit#toMillis(long) (since Java 5)

TimeUnit.NANOSECONDS.toMillis(end - start);

OR

Duration#toMillis() (since Java 8)

Duration.ofNanos(end - start).toMillis()

OR

Duration#between(Temporal, Temporal) (since Java 8)

Instant start = Instant.now();
...
Instant end = Instant.now();

Duration.between(start, end).toMillis()

OR

ChronoUnit.html#between(Temporal, Temporal) (since Java 8)

Instant start = Instant.now();
...
Instant end = Instant.now();

ChronoUnit.MILLIS.between(start, end)
YujiSoftware
  • 1,281
  • 14
  • 15
2

Just subtract them and divide result by 10^6.

1 nanosecond is 10^-9 seconds and, correspondingly, 10^-6 milliseconds.
http://en.wikipedia.org/wiki/Nano-

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
1

You could just use System.currentTimeMillis().

Caveat:

Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

harto
  • 89,823
  • 9
  • 47
  • 61
1

To get a meaningful result:

void procedure ( ... )
{
     ...
}

double measureProcedure ( double epsilon , ... )
{
    double mean ;
    double stderr = 2 * epsilon ;
    while ( stderr > epsilon )
    {
         long start = System.nanoTime();
         procedure ( ... ) ;
         long end = System.nanoTime();
         // recalculate mean , stderr 
    }
    return ( mean / 1000000 ) ;
}
emory
  • 10,725
  • 2
  • 30
  • 58