How to find time taken to run a Java program?

后端 未结 7 1849
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 06:17

I have a Java application that I\'ve been working on and I just realized that the program has to return a value in less than a minute, but don\'t know how to find or display

7条回答
  •  囚心锁ツ
    2020-12-24 06:44

    You can compare times using System.nanoTime() . It will return the time in nanoseconds.

    Returns the current value of the most precise available system timer, in nanoseconds.

    You could use it like this:

    long startTime = System.nanoTime();
    
    // code
    
    long endTime = System.nanoTime();
    System.out.println("Took "+(endTime - startTime) + " ns"); 
    

    Usefull links:

    • System.nanoTime()

提交回复
热议问题