java - Calculating time difference in Milliseconds

ID : 131335

viewed : 7

Tags : javajava

Top 5 Answer for java - Calculating time difference in Milliseconds

vote vote

99

Try this

long start_time = System.nanoTime(); resp = GeoLocationService.getLocationByIp(ipAddress); long end_time = System.nanoTime(); double difference = (end_time - start_time) / 1e6; 
vote vote

84

I pretty much like the (relatively) new java.time library: it's close to awesome, imho.

You can calculate a duration between two instants this way:

import java.time.*  Instant before = Instant.now(); // do stuff Instant after = Instant.now(); long delta = Duration.between(before, after).toMillis(); // .toWhatsoever() 

API is awesome, highly readable and intuitive.

Classes are thread-safe too. !


References: Oracle Tutorial, Java Magazine

vote vote

71

No, it doesn't mean it's taking 0ms - it shows it's taking a smaller amount of time than you can measure with currentTimeMillis(). That may well be 10ms or 15ms. It's not a good method to call for timing; it's more appropriate for getting the current time.

To measure how long something takes, consider using System.nanoTime instead. The important point here isn't that the precision is greater, but that the resolution will be greater... but only when used to measure the time between two calls. It must not be used as a "wall clock".

Note that even System.nanoTime just uses "the most accurate timer on your system" - it's worth measuring how fine-grained that is. You can do that like this:

public class Test {     public static void main(String[] args) throws Exception {          long[] differences = new long[5];         long previous = System.nanoTime();         for (int i = 0; i < 5; i++) {             long current;             while ((current = System.nanoTime()) == previous) {                 // Do nothing...             }             differences[i] = current - previous;             previous = current;                     }          for (long difference : differences) {             System.out.println(difference);         }     } } 

On my machine that shows differences of about 466 nanoseconds... so I can't possibly expect to measure the time taken for something quicker than that. (And other times may well be roughly multiples of that amount of time.)

vote vote

68

Since Java 1.5, you can get a more precise time value with System.nanoTime(), which obviously returns nanoseconds instead.

There is probably some caching going on in the instances when you get an immediate result.

vote vote

51

From Java 8 onward you can try the following:

import java.time.*; import java.time.temporal.ChronoUnit;  Instant start_time = Instant.now(); // Your code Instant stop_time = Instant.now();  System.out.println(Duration.between(start_time, stop_time).toMillis());  //or  System.out.println(ChronoUnit.MILLIS.between(start_time, stop_time)); 

Top 3 video Explaining java - Calculating time difference in Milliseconds

Related QUESTION?