對Java提供的兩個Map進行的性能測試

  對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));
  
  }
  };

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章