对java 提供的两个map 进行了性能测试发现效果还可以10万个key的map 查找 起来也不是很慢,大概50--60毫秒
还打算自己手工做些性能优化,将不同前缀的key分开到几个小map里 发现性能没有改观,纳闷中。。
分开到几个小map里,然后小的里面还可以再分,分分分,形成一个按字母检索树
突然想起,java pro 上有篇文章里说过map系列的内置 性能优化方式,好象就是和我说的这种思想是一致的
查找资料一看(http://www.fawcette.com/china/xmlfile.aspx?id=8&page=1),过真如此,哈哈,不过上次看了印象不深刻,这次自己想出来了,印象当真深刻的很,同时也证明了英雄所见略同(呵呵)
/**
* 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));
}
};
闽公网安备 35060202000074号