[英]common techniques to measure execution time provide different values (java)
我試圖在不同類型的集合上測量不同操作的時間並想比較它們,但我得到的值在相同類型的集合上有很大不同,比如因子 1000。我使用我在這里讀到的常用技術: 如何在 Java 中為方法的執行計時?
我比較了 Hashset、TreeSet 和 LinkedHashSet。 我用 1 000 000 個整數填充集合,使用方法 contains() 並遍歷集合。 我測量了每個操作的時間,數值相差很大。 所以我用相同類型的新集合第二次這樣做了,我得到的執行時間似乎不合法。
相同類型的集合需要一次 1400 毫秒然后 300 毫秒才能被填充。 這是為什么?
這是一個代碼示例,它可能會使我的意思更清楚:
public static void main(String[] args){
HashSet<Integer> firstHashSet = new HashSet<>(predefinedSize);
HashSet<Integer> secondHashSet = new HashSet<>(predefinedSize);
LinkedHashSet<Integer> firstLinkedHashSet = new LinkedHashSet<>(predefinedSize);
LinkedHashSet<Integer> secondLinkedHashSet = new LinkedHashSet<>(predefinedSize);
TreeSet<Integer> firstTreeSet = new TreeSet<>();
TreeSet<Integer> secondTreeSet = new TreeSet<>();
int x = 9432;
System.out.println("filling hashSet: <" + fillSet(firstHashSet) + "> milliSeconds");
System.out.println("filling linkedSet: <" + fillSet(firstLinkedHashSet) + "> milliSeconds");
System.out.println("filling treeSet: <" + fillSet(firstTreeSet) + "> milliSeconds");
System.out.println("-------------------------------------------------------------");
System.out.println("filling hashSet: <" + fillSet(secondHashSet) + "> milliSeconds");
System.out.println("filling linkedSet: <" + fillSet(secondLinkedHashSet) + "> milliSeconds");
System.out.println("filling treeSet: <" + fillSet(secondTreeSet) + "> milliSeconds");
這是我的一個圓角的樣子:
private static int size = 1000000;
private static int predefinedSize = 2000000;
public static double fillSet(LinkedHashSet<Integer> myHashSet){
double timeStart = System.nanoTime();
for(int i=0; i<size; i++){
myHashSet.add(i);
}
double time = (System.nanoTime() - timeStart)/ Math.pow(10, 6);
return time;
}
輸出是這樣的:
filling hashSet: <52.14022> milliSeconds
filling linkedSet: <95.599435> milliSeconds
filling treeSet: <2172.773956> milliSeconds
-------------------------------------------------------------
filling hashSet: <59.096929> milliSeconds
filling linkedSet: <1006.638126> milliSeconds
filling treeSet: <241.36395> milliSeconds
你看到輸出差異很大,我認為這取決於我電腦的計算能力,但我沒有在后台運行任何其他程序。 有人可以給我一個解釋和/或解決方案嗎?
正如@kan 的評論所提到的,使用系統計時器並執行一百萬次將提供截然不同的結果。 您正在尋找的是一個微基准:
至於您的時間安排無處不在的原因,您必須閱讀有關計算機體系結構和 Java JVM 的信息。 一些可能性:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.