long startTime = System.nanoTime();//code here...System.out.println("Code took "+(System.nanoTime()-startTime)+"nano seconds");
问题是,你不会在代码完成后立即获得结束时间。相反,请尝试以下操作:
final long endTime, startTime = System.nanoTime();//code here...endTime = System.nanoTime();System.out.println("Code took "+(endTime-startTime)+"nano seconds");