这是我的主要代码,它使用 nanoTime() 计算 selectionSort() 的时间差:
System.out.println("Start time :: " + test.start());
test.selectionSort();
System.out.println("End time :: " + test.end());
System.out.println(test);
System.out.print("Time took to run selectionSort() == ");
System.out.println((test.getElapsedTime()) + " nanoseconds");
我的 start()、end() 和 getElapsedTime() 方法是:
public long start(){
startTime = System.nanoTime();
return startTime;
}
public long end(){
endTime = System.nanoTime();
return endTime;
}
public long getElapsedTime(){
return end() - start();
}
运行此程序后,输出为:
5 2 9 7 4 3 0 1 6 8
Start time :: 915929737160723
End time :: 915929737309925
0 1 2 3 4 5 6 7 8 9
Time took to run selectionSort() == -5921 nanoseconds
getElapsedTime() 方法在它应该是正数时如何返回负数?