Time expended for HashMap get() is higher first ti

2019-09-09 10:43发布

问题:

I have been doing some performance checks with HashMap on Java. The results are surprising i should say. I would show the code first.

    public class HashMapCheck {
    int m_size =10;
    String Keys[], Values[] ;
    void  initKeyValues(int size){
      m_size=size;
      Keys = new String[m_size];
      Values = new String[m_size];
     for(int i=0;i<m_size;i++)
     {
         Keys[i]="key"+i;
         Values[i] ="value"+ i;
     }

    }
    public void  withOutInitialCapacity(){
    long start= System.nanoTime();
    HashMap hm = new HashMap();
    long hmEnd=System.nanoTime();
    long hmPutStart=hmEnd;
     for(int i=0;i<m_size;i++)
     {
        hm.put(Keys[i], Values[i]);
     }
     long hmPutEnd=System.nanoTime();
     long hmGetStart=hmPutEnd;
     hm.get("key5");
     hm.get("key155");
     hm.get("key195");
     //hm.get("key395");
     long hmGetEnd =System.nanoTime();
     long end= hmGetEnd;
     System.out.println("size="+m_size+",total="+(end-start)+",put="+
     (hmPutEnd-    hmPutStart)+",get="+(hmGetEnd-hmGetStart));

}

}

MAIN CLASS

    public class Driver {
    public static void main(String[] args) {
    HashMapCheck hmc = new HashMapCheck();
    hmc.initKeyValues(100);
    hmc.withOutInitialCapacity();

    HashMapCheck hmc2 = new HashMapCheck();
    hmc2.initKeyValues(100);
    hmc2.withOutInitialCapacity();

    HashMapCheck hmc3 = new HashMapCheck();
    hmc3.initKeyValues(100);
    hmc3.withOutInitialCapacity();

    HashMapCheck hmc4 = new HashMapCheck();
    hmc4.initKeyValues(100);
    hmc4.withOutInitialCapacity();

    HashMapCheck hmc5 = new HashMapCheck();
    hmc5.initKeyValues(100);
    hmc5.withOutInitialCapacity();

    HashMapCheck hmc6 = new HashMapCheck();
    hmc6.initKeyValues(100);
    hmc6.withOutInitialCapacity();
    }
    }

OK I am measuring time taken to put 100 values in a hashmap and get 3 values from them. Now comes the question.

1) I first tried with the above function where 1 value(key5) is in hashMap and 2 values(key155,key195) doesnt exist in hashmap. following is the result size=100,total=80446,put=69190,get=6290 size=100,total=69852,put=63893,get=2317 size=100,total=69852,put=62900,get=3972 size=100,total=68859,put=64224,get=1656 size=100,total=69190,put=63562,get=1655 size=100,total=67866,put=63562,get=1324 as you can see get() time in 3rd and first is higher than all the other and the total is also high for first

2)This time i tried with a slight change made to withoutInitialCapacity(). This time i use hm.get("key5"); hm.get("key45"); hm.get("key195"); So two values is available in hashMap and one value isn't. Result is

size=100,total=80777,put=68859,get=6952 size=100,total=73825,put=63893,get=5959
size=100,total=82763,put=78459,get=1324 size=100,total=70845,put=66210,get=1656
size=100,total=70845,put=64886,get=1655 size=100,total=69190,put=64886,get=1324
1st and second get has a high value. 

Why is this happening? Is it JIT?