One - One Code All

Blog Content

java计算程序运行的时间

Java   2008-04-30 23:37:54
public class Main {  
  
    /** 
    * 计算两个时间点直接逝去的毫秒数 
    * 
    */  
    public void computeAndDisplayElapsedTime() {  
      
        long startTime = System.currentTimeMillis();  
          
        for (int i = 0; i < 10; i++) {  
            try {         
                Thread.sleep(60);  
          
            } catch (InterruptedException ex) {  
                ex.printStackTrace();  
            }  
        }  
          
        long endTime = System.currentTimeMillis();  
          
        float seconds = (endTime - startTime) / 1000F;  
      
        System.out.println(Float.toString(seconds) + " seconds.");  
      
    }  
      
    /** 
    * 启动程序 
    */  
    public static void main(String[] args) {  
        new Main().computeAndDisplayElapsedTime();  
    }  
}



上一篇:java格式化输出System.out.format()
下一篇:java的Stack.peek()和Stack.pop()的区别

The minute you think of giving up, think of the reason why you held on so long.