1 long start = System.currentTimeMillis(); // 記錄起始時間 2 try { 3 Thread.sleep(5000); // 線程睡眠5秒,讓運行時間不那么小 4 } catch (InterruptedException e) { 5 e.printStackTrace(); 6 } 7 long end = System.currentTimeMillis(); // 記錄結束時間 8 System.out.println(end-start); // 相減得出運行時間
單位是毫秒。
long start = System.currentTimeMillis(); //獲取當前時間
long end = System.currentTimeMillis();
//System.nanoTime()
