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

前端 未结 9 577
温柔的废话
温柔的废话 2020-12-02 10:09

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

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


        
9条回答
  •  感动是毒
    2020-12-02 10:17

    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");
    

提交回复
热议问题