关于一个大map和多个小map的性能对比

    xiaoxiao2021-12-14  19

    话不多说,看结果  10万条数据,两种方法基本上差不多 单位毫秒 录入时间为:2226 查找时间为:22 ----- 录入时间为432 查找时间为:13 import java.util.*; public class MapTest2 { public static void main(String ags[]) { test2(); System.out.println("-----"); test1(); } public static void test1() { Map m = new HashMap(100000); long t0 = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { m.put("aa.bb.to.pub." + i + "12345asfsdfVO", "value"); } long t1 = System.currentTimeMillis(); System.out.println(t1 - t0); //随机取出1000个数字 for (int i = 0; i < 10000; i++) { int j = new Random(10000).nextInt(); m.get("aa.bb.to.pub." + j + "12345asfsdfVO"); } long t2 = System.currentTimeMillis(); System.out.println(t2 - t1); } public static void test2() { Map m = new HashMap(10); m.put("aa.bb.ao", new HashMap(10000)); m.put("aa.bb.bo", new HashMap(10000)); m.put("aa.bb.co", new HashMap(10000)); m.put("aa.bb.do", new HashMap(10000)); m.put("aa.bb.eo", new HashMap(10000)); m.put("aa.bb.fo", new HashMap(10000)); m.put("aa.bb.go", new HashMap(10000)); m.put("aa.bb.ho", new HashMap(10000)); m.put("aa.bb.io", new HashMap(10000)); m.put("aa.bb.jo", new HashMap(10000)); long t0 = System.currentTimeMillis(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10000; j++){ // 每个模块里有1000个 ((Map) m.get("aa.bb.ao")).put("aa.bb.to.pub." + j+ "12345asfsdfVO", "value"); ((Map) m.get("aa.bb.bo")).put("aa.bb.to.pub." + j+ "12345asfsdfVO", "value"); ((Map) m.get("aa.bb.co")).put("aa.bb.to.pub." + j+ "12345asfsdfVO", "value"); ((Map) m.get("aa.bb.do")).put("aa.bb.to.pub." + j+ "12345asfsdfVO", "value"); ((Map) m.get("aa.bb.eo")).put("aa.bb.to.pub." + j+ "12345asfsdfVO", "value"); ((Map) m.get("aa.bb.fo")).put("aa.bb.to.pub." + j+ "12345asfsdfVO", "value"); ((Map) m.get("aa.bb.go")).put("aa.bb.to.pub." + j+ "12345asfsdfVO", "value"); ((Map) m.get("aa.bb.ho")).put("aa.bb.to.pub." + j+ "12345asfsdfVO", "value"); ((Map) m.get("aa.bb.io")).put("aa.bb.to.pub." + j+ "12345asfsdfVO", "value"); ((Map) m.get("aa.bb.jo")).put("aa.bb.to.pub." + j+ "12345asfsdfVO", "value"); } } long t1 = System.currentTimeMillis(); System.out.println("录入时间为:" + (t1 - t0));; // System.out.println(m.get("8888key")); for (int i = 1; i < 10000; i++) { int j = new Random(10000).nextInt(); ((Map) m.get("aa.bb.ao")).get("aa.bb.to.pub." + j+ "12345asfsdfVO"); } long t2 = System.currentTimeMillis(); System.out.println("查找时间为:" + (t2 - t1)); } }
    转载请注明原文地址: https://ju.6miu.com/read-965268.html

    最新回复(0)