我得到不同的执行时间,如果我交换HashMap和HashSet。首先出现的执行时间总是很高(HashMap/Hashset)。我不确定背后的原因。任何帮助表示赞赏根据执行顺序对HashMap和HashSet执行不同的执行时间?
执行1 - HashMap的第一个,然后HashSet的--- 所用时间地图地址:2071ms, 时间拍摄的这组加载:794ms
执行2 - HashSet的第一,然后HashMap的--- 时间采取集中添加:2147ms, 所用时间地图地址:781ms
private static Random secureRandom = new SecureRandom();
public static void main(String args[])
{
int testnumber = 1000000;
// HashMap
long starttimemap = System.currentTimeMillis();
Map<String, String> hashmap = new HashMap<String, String>();
for (int i = 0; i < testnumber; i++)
{
hashmap.put(Long.toHexString(secureRandom.nextLong()), "true");
}
long endtimemap = System.currentTimeMillis();
System.out.println("Time taken map add: " + (endtimemap - starttimemap) + "ms");
// HashSet
long starttimeset = System.currentTimeMillis();
Set<String> hashset = new HashSet<String>();
for (int i = 0; i < testnumber; i++)
{
hashset.add(Long.toHexString(secureRandom.nextLong()));
}
long endtimeset = System.currentTimeMillis();
System.out.println("Time taken set add: " + (endtimeset - starttimeset) + "ms");
}