感觉速度相差不大呢?甚至调成100万1000万测试发现反倒不如默认16的速度呢?时代进步了还是我代码写错了?
package com.barry;
import java.util.HashMap;
import java.util.Map;
/**
* @Author BarryFitzgerald
* @Date 2023/4/16 19:35
* @Description:
* 1、创建10个HashMap,每个HashMap含有10万条记录;
* 2、传递不同的构造方法的参数比较速度;
* 3、构造方法参数分别使用(16,0.75f)和(16384,0.75)
*/
public class TestMapWithParameter {
public static void main(String[] args) {
long sum = 0L;
for(int i=0;i<10;i++){
sum +=new TestMapWithParameter().inputMap(100000,0.75f);
}
System.out.println(sum/10);
}
public Long inputMap(int initialCapacity,float loadFactor){
String key,value;
Map<String, String> testMap = new HashMap<String, String>(initialCapacity,loadFactor);
long start = System.nanoTime();
for(int i=0;i<100000;i++){
key = String.valueOf(i);
value = "value";
testMap.put(key,value);
}
long end = System.nanoTime();
long time = end - start;
System.out.println("creating new hashmap using:"+time);
return time;
}
}