Search code examples
javamilliseconds

How to measure the a time-span in seconds using System.currentTimeMillis()?


How to convert System.currentTimeMillis(); to seconds?

long start6=System.currentTimeMillis();
System.out.println(counter.countPrimes(100000000)+" for "+start6);

The console shows me 5761455 for 1307816001290. I can't read how many seconds that is.

Any help?


Solution

  • long start = System.currentTimeMillis();
    counter.countPrimes(1000000);
    long end = System.currentTimeMillis();
    
    System.out.println("Took : " + ((end - start) / 1000));
    

    UPDATE

    An even more accurate solution would be:

    final long start = System.nanoTime();
    counter.countPrimes(1000000);
    final long end = System.nanoTime();
    
    System.out.println("Took: " + ((end - start) / 1000000) + "ms");
    System.out.println("Took: " + (end - start)/ 1000000000 + " seconds");