這兩個自定義比較器中哪一個效率最高? (用在數千陣列的矢量)自定義比較器的效率
//Compares arrays by their first element, then second if equal
Comparator<String[]> comparator = new Comparator<String[]>() {
public int compare(String[] c1, String[] c2) {
int comparison;
if((comparison=(c1[0].compareTo(c2[0])))==0) return c1[1].compareTo(c2[1]);
else return comparison;
}
};
//Compares arrays by their first element, then second if equal
Comparator<String[]> comparator = new Comparator<String[]>() {
public int compare(String[] c1, String[] c2) {
if(c1[0].compareTo(c2[0])==0) return c1[1].compareTo(c2[1]);
else return c1[0].compareTo(c2[0]);
}
}
您是否嘗試過對它們進行基準測試? – JonK
或看到這些甚至被編譯成不同的字節碼? – pamphlet
也許你應該去問代碼審查? http://codereview.stackexchange.com/ – 1337