Two simple methods for Java program execution time

  • 2020-04-01 02:17:08
  • OfStack

The first is measured in milliseconds.   Java code   // pseudocode    


long startTime=System.currentTimeMillis();   //Getting the start time & NBSP;  
doSomeThing();  //Test code segment & NBSP;  
long endTime=System.currentTimeMillis(); //Getting the end time & NBSP;  
System.out.println(" Program running time:  "+(end-start)+"ms");  

The second is measured in nanoseconds.   Java code   // pseudocode    

long startTime=System.nanoTime();   //Getting the start time & NBSP;  
doSomeThing();  //Test code segment & NBSP;  
long endTime=System.nanoTime(); //Getting the end time & NBSP;  
System.out.println(" Program running time:  "+(end-start)+"ns");  


Related articles: