标签:style asm keyset 性能对比 遍历 long out ati stat
使用KeySet和EntrySet遍历的差别
1 public static void main(String[] args) { 2 3 HashMap<Integer, Integer> hasMap = new HashMap(); 4 for (int i = 0; i < 10000; i++) { 5 hasMap.put(i, i); 6 } 7 8 Long j = System.currentTimeMillis(); 9 for (int i = 1; i < 100000; i++) { 10 for (Map.Entry<Integer, Integer> integerIntegerEntry : hasMap.entrySet()) { 11 integerIntegerEntry.getKey(); 12 integerIntegerEntry.getValue(); 13 } 14 } 15 System.out.println("使用EntrySet:" + (System.currentTimeMillis() - j)); 16 17 Long k = System.currentTimeMillis(); 18 for (int i = 1; i < 100000; i++) { 19 for (Integer key : hasMap.keySet()) { 20 hasMap.get(key); 21 } 22 } 23 System.out.println("使用KeySet:" + (System.currentTimeMillis() - k)); 24 25 }
运行多次后,两者差别有2秒左右
结论:使用EntrySet遍历时性能更高
原因分析:。。。
标签:style asm keyset 性能对比 遍历 long out ati stat
原文地址:https://www.cnblogs.com/yszzu/p/9410741.html