作为学校练习的一部分,我想将排序算法作为Java练习进行比较和对比.
我自己实现了排序算法,并对Person实现Comparable接口的类的对象进行排序.
到目前为止这么好,但我无法解释的是为什么在第一次调用我的排序方法时,排序比后续调用需要更长的时间?
下面的输出代表我的结果.
Best,Worst和Avg是指传递给排序方法的未排序数组:
这是我的输出:
1-call of the sorting methods
InsertionSort Best:1799ms Worst:78ms Avg:789ms
MergeSort Best:10ms Worst:3ms Avg:5ms
2-call of the sorting methods
InsertionSort Best:1065ms Worst:39ms Avg:691ms
MergeSort Best:3ms Worst:2ms Avg:5ms
3-call of the sorting methods
InsertionSort Best:1066ms Worst:39ms Avg:692ms
MergeSort Best:3ms Worst:2ms Avg:5ms
4-call of the sorting methods
InsertionSort Best:1065ms Worst:39ms Avg:691ms
MergeSort Best:3ms Worst:2ms Avg:5ms
Run Code Online (Sandbox Code Playgroud)
JVM是否对后续调用进行了任何优化?
我很困惑,非常感谢任何帮助!
编辑:感谢您的建议和答案到目前为止!要清楚几点 - 我的输出中的每个调用都指的是完成排序所需的时间!
每次排序后,我再次使用UNSORTED阵列进行新的调用!
我的源代码可以作为zip文件下载为eclipse项目,位于以下Dropbox链接: dropbox链接到eclipse project.zip
PS我没有使用剖析器的经验 - 如果你能指点我一个教程,那就太好了.