java中運行一段代碼所需時間實現方式

package csdn;


public class 一段代碼運行時間 {

public static void main(String[] args) {

// TODO Auto-generated method stub

//首先獲取開始運行的時間,用系統函數System.currentTimeMillis()獲取,以毫秒爲單位

long startTime = System.currentTimeMillis();
long s = 0;
for(int i = 0; i < 100000;i++){
s = i + s;
}
System.out.println("s"+s);
long endTime = System.currentTimeMillis();
System.out.println(endTime -startTime+"ms");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章