6月12號做題總結

一:兩種方式效率比較

1:

for (int i = 0; i < n; i++) {
    String sp = bf.readLine();
    if (sp.charAt(0)=='i'){
        hashSet.add(sp.substring(7));
    }else {
        System.out.println(hashSet.contains(sp.substring(5))?"yes":"no");
    }
}
2:

for (int i = 0; i < n; i++) {
    String[] sp = bf.readLine().split(" ");
    if (sp[0].equals("insert")){
        hashSet.add(sp[1]);
    }else {
        System.out.println(hashSet.contains(sp[1])?"yes":"no");
    }
}

結論 1的效率比2高,當1000000級別數據進行試驗,速度大約提高0.3秒左右;


StringBuilder比System.out.println()效率要很多很多
結論 當1000000級別數據進行試驗,速度大約提高1秒左右;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章