我得到不同的執行時間,如果我交換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");
}