给定以下代码,有两种替代方法来遍历它,
这两种方法之间有什么性能差异吗?
Map<String, Integer> map = new HashMap<String, Integer>();
//populate map
//alt. #1
for (String key : map.keySet())
{
Integer value = map.get(key);
//use key and value
}
//alt. #2
for (Map.Entry<String, Integer> entry : map.entrySet())
{
String key = entry.getKey();
Integer value = entry.getValue();
//use key and value
}
我倾向于认为 alt. #2
是遍历整个 map
的更有效方法(但我可能错了)
原文由 bguiz 发布,翻译遵循 CC BY-SA 4.0 许可协议
您的第二个选项肯定更有效,因为与第一个选项中的 n 次相比,您只进行一次查找。
但是,没有什么比在可能的时候尝试一下更好的了。所以这里去 -
(不完美但足以验证假设并且无论如何在我的机器上)
结果(一些有趣的)
随着
int mapSize = 5000; int strLength = 5;
Alt #1 耗时 26 毫秒
Alt #2 耗时 20 毫秒
随着
int mapSize = 50000; int strLength = 5;
Alt #1 耗时 32 毫秒
Alt #2 耗时 20 毫秒
随着
int mapSize = 50000; int strLength = 50;
Alt #1 耗时 22 毫秒
Alt #2 耗时 21 毫秒
随着
int mapSize = 50000; int strLength = 500;
Alt #1 耗时 28 毫秒
Alt #2 耗时 23 毫秒
随着
int mapSize = 500000; int strLength = 5;
Alt #1 耗时 92 毫秒
Alt #2 耗时 57 毫秒
…等等