HashMap的clear()方法和new HashMap的效率问题
最近研究Lucene的时候,遇到的用到大量Map的问题,心生好奇,想看一下在1W,10W,100W三种数据量下,new HashMap ,与 HashMap.clear()方法的效率问题。
提前说明:就是自己好奇测着玩的,不能作为任何依据,只是提供一个参考。
JDK版本:1.8
IDE:idea
Clear()
数据量级:1W ,执行次数:10次,取10次时间的平均值
执行结果:
map-clear:27 map-clear:27 map-clear:15 map-clear:14 map-clear:15 map-clear:13 map-clear:11 map-clear:3 map-clear:4 map-clear:4 平均时间:13
执行内存快照:
数据量级:10W ,执行次数:10次,取10次时间的平均值
执行结果:
map-clear:212 map-clear:128 map-clear:99 map-clear:161 map-clear:49 map-clear:58 map-clear:44 map-clear:112 map-clear:218 map-clear:40 平均时间:112
执行内存快照:
数据量级:100W ,执行次数:10次,取10次时间的平均值
执行结果:
map-clear:661 map-clear:657 map-clear:364 map-clear:426 map-clear:343 map-clear:481 map-clear:271 map-clear:212 map-clear:176 map-clear:165 平均时间:375
执行内存快照:
new HashMap()
数据量级:1W ,执行次数:10次,取10次时间的平均值
执行结果:
map-new:27 map-new:20 map-new:10 map-new:13 map-new:19 map-new:21 map-new:9 map-new:7 map-new:7 map-new:6 平均时间:13
执行内存快照:
数据量级:10W ,执行次数:10次,取10次时间的平均值
执行结果:
map-new:122 map-new:58 map-new:66 map-new:110 map-new:83 map-new:45 map-new:62 map-new:36 map-new:29 map-new:30 平均时间:64
执行内存快照:
数据量级:100W ,执行次数:10次,取10次时间的平均值
执行结果:
map-new:539 map-new:493 map-new:420 map-new:294 map-new:455 map-new:305 map-new:379 map-new:256 map-new:314 map-new:315 平均时间:377
执行内存快照:
测试类:
public class MapTest { public static void main(String[] args) throws InterruptedException { long totalTimes = 0L; for (int i = 0 ; i < 10 ; i++){ totalTimes += mapToNew(1000000L); } System.out.println("平均时间:" + (totalTimes / 10)); Thread.sleep(100000L); } /** * new map * @param size * @return */ public static long mapToNew(long size) { long nowTime = new Date().getTime(); Map<String,Object> map; for (int i = 0 ; i < size ; i++){ map = new HashMap<>(); map.put("id",i); map.put("name","测试文章"); map.put("realname","测试文章11111"); map.put("text","测试文章+++++++++++++++++++"); map.put("long",4324L); map.put("double",4354D); map.put("text2","测试文章+++++++++++++++++++"); map.put("text3","测试文章+++++++++++++++++++"); map.put("text4","测试文章+++++++++++++++++++"); map.put("text5","测试文章+++++++++++++++++++"); map.put("text6","测试文章+++++++++++++++++++"); map.put("text7","测试文章+++++++++++++++++++"); } long timeSize = new Date().getTime() - nowTime; System.out.println("map-new:" + timeSize); return timeSize; } /** * clear map * @param size * @return */ public static long mapToClear(long size) { long nowTime = new Date().getTime(); Map<String,Object> map = new HashMap<>(); for (int i = 0 ; i < size ; i++){ map.clear(); map.put("id",i); map.put("name","测试文章"); map.put("realname","测试文章11111"); map.put("text","测试文章+++++++++++++++++++"); map.put("long",4324L); map.put("double",4354D); map.put("text2","测试文章+++++++++++++++++++"); map.put("text3","测试文章+++++++++++++++++++"); map.put("text4","测试文章+++++++++++++++++++"); map.put("text5","测试文章+++++++++++++++++++"); map.put("text6","测试文章+++++++++++++++++++"); map.put("text7","测试文章+++++++++++++++++++"); } long timeSize = new Date().getTime() - nowTime; System.out.println("map-clear:" + timeSize); return timeSize; } }