Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1786427
  • 博文数量: 1647
  • 博客积分: 80000
  • 博客等级: 元帅
  • 技术积分: 9980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 15:15
文章分类

全部博文(1647)

文章存档

2011年(1)

2008年(1646)

我的朋友

分类:

2008-10-28 18:22:39

    对java 提供的两个Map 进行了性能测试发现效果还可以10万个key的Map 查找 起来也不是很慢,大概50--60毫秒还打算自己手工做些性能优化,将不同前缀的KEY分开到几个小MAP里发现性能没有改观,纳闷中。 分开到几个小MAP里,然后小的里面还可以再分,分分分,形成一个按字母检索树突然想起, Pro 上有篇文章里说过Map系列的内置 性能优化方式,好象就是和我说的这种思想是一致的 查找资料一看(),过真如此,哈哈,不过上次看了印象不深刻,这次自己想出来了,印象当真深刻的很,同时也证明了英雄所见略同

    /**
    * Map 系列性能测试
    */
    import java.util.*;
    public class MapTest
    {
    public static void main(String ags[]){
    test2();
    System.out.println("-----");
    test1();
    }
    public static void test1(){
    Map m = new HashMap();
    long t0 = System.currentTimeMillis();
    for (int i = 0; i < 99999 ; i++)
    {
    m.put("aa.bb.to.pub."+i+"12345asfsdfVO",i+"value");
    }
    long t1 = System.currentTimeMillis() ;
    System.out.println(t1-t0);;
    //System.out.println(m.get("8888key"));
    for (int i = 0; i < 99999 ; i++)
    {
    m.get("aa.bb.to.pub."+i+"12345asfsdfVO");
    }
    long t2 = System.currentTimeMillis() ;
    System.out.println(t2-t1);

    }
    public static void test2(){
    Map m = new HashMap();
    m.put("aa.bb.ao",new HashMap());
    m.put("aa.bb.do",new HashMap());
    m.put("aa.bb.wo",new HashMap());
    m.put("aa.bb.po",new HashMap());
    m.put("aa.bb.io",new HashMap());
    m.put("aa.bb.oo",new HashMap());
    m.put("aa.bb.bo",new HashMap());
    m.put("aa.bb.to",new HashMap());
    m.put("aa.bb.yo",new HashMap());
    m.put("aa.bb.ro",new HashMap());
    long t0 = System.currentTimeMillis();
    for (int i = 10; i < 20 ; i++)
    {
    for (int k = 1; k < 5 ; k++) //有10个模块,比较5个模块概率
    if("aa.bb.to.pub.12345headerVO".startsWith("aa.bb.to"));
    //下面假设上边比较结果为: aa.bb.to开头
    for(int j = 1000; j < 2000; j++) //每个模块里有1000个
    ((Map)m.get("aa.bb.to")).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 = 10; i < 20 ; i++)
    {
    for (int k = 1; k < 5 ; k++) //有10个模块,比较5个模块概率
    if("aa.bb.to.pub.12345asfsdfVO".startsWith("aa.bb.to"));

    for(int j = 1000; j < 2000; j++)
    ((Map)m.get("aa.bb.to")).get("aa.bb.to.pub."+j+"12345asfsdfVO");
    }
    long t2 = System.currentTimeMillis() ;
    System.out.println("查找时间为:"+(t2-t1));

    }
    };

【责编:Ken】

--------------------next---------------------

阅读(208) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~