①精確到納秒
long startTime=System.nanoTime();//獲取開始時間
/*
測試的程序代碼;
*/
long endTime=System.nanoTime();//獲取結束時間
System.out.println("運行時間是:"+(endTime-startTime)+"ns");//輸出運行時間
②精確到毫秒
long startTime=System.currentTimeMillis();//獲取開始時間
/*
測試程序代碼;
*/
long endTime=System.currentTimeMillis();//獲取結束時間
System.out.println("運行時間是:"+(endTime-startTime)+"ms");//輸出運行時間