?
Solved

Hashtable Vs Hashmap

Posted on 2004-08-28
23
Medium Priority
?
1,308 Views
Last Modified: 2011-09-20
Hello All,

I just wanted to know which is faster.

In terms of putting the key/value pair and also accessing them.

thanks
sudhakar
0
Comment
Question by:sudhakar_koundinya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 7
  • 3
  • +1
23 Comments
 
LVL 35

Assisted Solution

by:girionis
girionis earned 150 total points
ID: 11923387
HashMap is faster since it is unsynchronized.
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11923392
ok let me test that
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11923458
public final static Object[] getUrlList8(java.util.List urls) {
            //Hashtable hostToUrls = new Hashtable(urls.size());
            HashMap hostToUrls = new HashMap(urls.size());
            StringBuffer accountBuffer = new StringBuffer(32);
            for (int i = 0; i < urls.size(); i++) {
                  accountBuffer.setLength(0);
                  String url = (String) urls.get(i);
                  // System.err.println(url);
                  int forwardSlashCount = 0;
                  int hostHashCode = 0;

                  for (int ixChar = 4; forwardSlashCount < 5 && ixChar < url.length(); ixChar++) {
                        // for (int ixChar = 4; forwardSlashCount < 4 ; ixChar++) {

                        if (url.charAt(ixChar) == '/') {
                              forwardSlashCount++;
                        }
                        if (forwardSlashCount == 2) {
                              // Compute hashcode of host
                              hostHashCode = 3 * hostHashCode + url.charAt(ixChar);
                        } else if (forwardSlashCount == 4) {
                              accountBuffer.append(url.charAt(ixChar));
                        }
                  }
                  // end for

                  accountBuffer.append(url.charAt(4)).append(hostHashCode);
                  //   System.out.println(accountBuffer) ;
                  final String key = accountBuffer.toString();
                  ArrayList urlsForHost = (ArrayList) hostToUrls.get(key);
                  if (urlsForHost == null) {
                        urlsForHost = new ArrayList();
                        urlsForHost.add(url);
                        hostToUrls.put(key, urlsForHost);
                  } else {
                        urlsForHost.add(url);
                  }
            }
            // Enumeration e= hostToUrls.keys();
            //   while(e.hasMoreElements())
              //  System.err.println(e.nextElement());
            //return Collections.list(hostToUrls.values());
            return hostToUrls.values().toArray();
      }



public final static List getUrlList9(java.util.List urls) {
            //Hashtable hostToUrls = new Hashtable(urls.size());
            Hashtable hostToUrls = new Hashtable(urls.size());
            StringBuffer accountBuffer = new StringBuffer(32);
            for (int i = 0; i < urls.size(); i++) {
                  accountBuffer.setLength(0);
                  String url = (String) urls.get(i);
                  // System.err.println(url);
                  int forwardSlashCount = 0;
                  int hostHashCode = 0;

                  for (int ixChar = 4; forwardSlashCount < 5 && ixChar < url.length(); ixChar++) {
                        // for (int ixChar = 4; forwardSlashCount < 4 ; ixChar++) {

                        if (url.charAt(ixChar) == '/') {
                              forwardSlashCount++;
                        }
                        if (forwardSlashCount == 2) {
                              // Compute hashcode of host
                              hostHashCode = 3 * hostHashCode + url.charAt(ixChar);
                        } else if (forwardSlashCount == 4) {
                              accountBuffer.append(url.charAt(ixChar));
                        }
                  }
                  // end for

                  accountBuffer.append(url.charAt(4)).append(hostHashCode);
                  //   System.out.println(accountBuffer) ;
                  final String key = accountBuffer.toString();
                  ArrayList urlsForHost = (ArrayList) hostToUrls.get(key);
                  if (urlsForHost == null) {
                        urlsForHost = new ArrayList();
                        urlsForHost.add(url);
                        hostToUrls.put(key, urlsForHost);
                  } else {
                        urlsForHost.add(url);
                  }
            }
            // Enumeration e= hostToUrls.keys();
            //   while(e.hasMoreElements())
              //  System.err.println(e.nextElement());
            return Collections.list(hostToUrls.elements());
            
      }


When I tested  above two methods I got same results :-(

public static void main(String[] args) {
            // TODO code application logic here
            Vector vect = new Vector();
            ListArrayTest test = new ListArrayTest();
            vect = test.getURLS();
            java.util.Date d = new java.util.Date();
            //List l[]=test.getURLList(vect);
            Object l[] = test.getUrlList8(vect);
      //      List l = test.getUrlList9(vect);
            //for (int i = 0; i < l.size(); i++) {
                  for (int i = 0; i < l.length; i++) {
                  //System.out.println("List :" + i + " \t");

            //      List ll = (List) l.get(i);
                        List ll = (List) l[i];
                  // System.out.println("\t"+ll.get(0));
                  for (int j = 0; j < ll.size(); j++) {
                        //System.out.println("\t" + ll.get(j));
                  }
            }
            //List l[]=test.getURLList1(vect);
            //List l[]=test.getUrlList(vect);
            java.util.Date d1 = new java.util.Date();
            System.err.println(d1.getTime() - d.getTime());

      }

Here is the test case

public Vector getURLS() {
            Vector array=new Vector();
            for(int i=0;i <100;i++)
            {
                  for(int j=0;j<1000;j++)
                  {
                        array.add("http://Test"+i+"/exchange/"+(i+j));
                         
                  }
            }
      

0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11923518
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11923532
0
 
LVL 35

Expert Comment

by:girionis
ID: 11923543
> When I tested  above two methods I got same results :-(

Exactly the same?
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11923587
yes


 one interesting case is when I tested the following code

import java.util.*;
public class HashTest
{
        public static void main(String s[])
      {
              int i=0;
            Date start=new Date();
            Hashtable ht=new Hashtable();
            for( i=0;i<100000;i++)
            {
               ht.put(new Integer(i),new Integer(i));
            }
            ht=null;
            Date end=new Date();
            System.err.println(end.getTime()-start.getTime());
            
             start=new Date();
             
            HashMap hm=new HashMap();
            for( i=0;i<100000;i++)
            {
               hm.put(new Integer(i),new Integer(i));
            }
            hm=null;
            end=new Date();
            System.err.println(end.getTime()-start.getTime());
            
      }
}


Hashtable Time for putting key value pairs :234
Hashmap Time for putting key value pairs :359

So Hashmap took more time although it is unsynchronized

0
 
LVL 35

Expert Comment

by:girionis
ID: 11923628
I ran the above code ten times on Windows 2000 with 1.2GHz AMD Athlon with jdk1.4.0_03. The results are:

Hashtable: 611, 601, 611, 621, 610, 611, 621, 621, 611, 621
HashMap: 520, 511, 521, 541, 531, 531, 531, 531, 531, 521

0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11923670
Windows XP- P4 1.2 Ghz and Jdk 1.4

import java.util.*;
public class HashTest
{
        public static void main(String s[])
      {
           for(int k=0;k<10;k++)
           {
           int i=0;
            Date start=new Date();
            Hashtable ht=new Hashtable();
            for( i=0;i<100000;i++)
            {
               Integer ii=new Integer(i);
               ht.put(ii,ii);
            }
            ht=null;
            Date end=new Date();
            System.err.println("Hashtable Time for putting key value pairs :"+(end.getTime()-start.getTime()));
            
             start=new Date();
             
            HashMap hm=new HashMap();
            for( i=0;i<100000;i++)
            {
                  Integer ii=new Integer(i);
               hm.put(ii,ii);
            }
            hm=null;
            end=new Date();
            System.err.println("Hashmap Time for putting key value pairs :"+(end.getTime()-start.getTime()));
           }
            
      }
}



Hashtable Time for putting key value pairs :235
Hashmap Time for putting key value pairs :359
Hashtable Time for putting key value pairs :187
Hashmap Time for putting key value pairs :328
Hashtable Time for putting key value pairs :188
Hashmap Time for putting key value pairs :328
Hashtable Time for putting key value pairs :187
Hashmap Time for putting key value pairs :344
Hashtable Time for putting key value pairs :188
Hashmap Time for putting key value pairs :343
Hashtable Time for putting key value pairs :188
Hashmap Time for putting key value pairs :359
Hashtable Time for putting key value pairs :203
Hashmap Time for putting key value pairs :328
Hashtable Time for putting key value pairs :204
Hashmap Time for putting key value pairs :343
Hashtable Time for putting key value pairs :188
Hashmap Time for putting key value pairs :344
Hashtable Time for putting key value pairs :187
Hashmap Time for putting key value pairs :328
0
 
LVL 35

Expert Comment

by:girionis
ID: 11923673
0
 
LVL 35

Accepted Solution

by:
girionis earned 150 total points
ID: 11923679
0
 
LVL 35

Expert Comment

by:girionis
ID: 11923684
That's weird. Tests in the above links show that sometimes HashMap is faster and sometimes Hashtable is faster. I was under the impression that HashMap was always faster than Hashtable when synchronization is not an issue.
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11923791
for the following code

the results are like this :-)

import java.util.*;
public class HashTest
{
        public static void main(String s[])
      {
           for(int k=0;k<10;k++)
           {
            new myThread(k).start();
           }
            
      }
}

class myThread extends Thread
{
      int kk=0;
      public myThread(int i)
      {
            this.kk=i;
      }
      public void run()
       {
             int i=0;
            Date start=new Date();
            Hashtable ht=new Hashtable();
            for( i=0;i<100000;i++)
            {
               Integer ii=new Integer(i);
               ht.put(ii,ii);
            }
            ht=null;
            Date end=new Date();
            System.err.println("Thread "+kk+" : Hashtable Time for putting key value pairs :"+(end.getTime()-start.getTime()));
            
             start=new Date();
             
            HashMap hm=new HashMap();
            for( i=0;i<100000;i++)
            {
                  Integer ii=new Integer(i);
               hm.put(ii,ii);
            }
            hm=null;
            end=new Date();
            System.err.println("Thread "+kk+" : HashMap Time for putting key value pairs :"+(end.getTime()-start.getTime()));
       }
}



Thread 0 : Hashtable Time for putting key value pairs :360
Thread 4 : Hashtable Time for putting key value pairs :250
Thread 8 : Hashtable Time for putting key value pairs :234
Thread 1 : Hashtable Time for putting key value pairs :954
Thread 3 : Hashtable Time for putting key value pairs :2032
Thread 0 : HashMap Time for putting key value pairs :1781
Thread 3 : HashMap Time for putting key value pairs :687
Thread 2 : Hashtable Time for putting key value pairs :2891
Thread 4 : HashMap Time for putting key value pairs :2250
Thread 1 : HashMap Time for putting key value pairs :2468
Thread 9 : Hashtable Time for putting key value pairs :703
Thread 2 : HashMap Time for putting key value pairs :875
Thread 6 : Hashtable Time for putting key value pairs :2937
Thread 6 : HashMap Time for putting key value pairs :516
Thread 9 : HashMap Time for putting key value pairs :797
Thread 8 : HashMap Time for putting key value pairs :3657
Thread 7 : Hashtable Time for putting key value pairs :3844
Thread 7 : HashMap Time for putting key value pairs :312
Thread 5 : Hashtable Time for putting key value pairs :250
Thread 5 : HashMap Time for putting key value pairs :359
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11923801
I mean

Thread 0 : HashMap Time for putting key value pairs :1781
Thread 0 : Hashtable Time for putting key value pairs :360
Thread 1 : HashMap Time for putting key value pairs :2468
Thread 1 : Hashtable Time for putting key value pairs :954
Thread 2 : HashMap Time for putting key value pairs :875
Thread 2 : Hashtable Time for putting key value pairs :2891
Thread 3 : HashMap Time for putting key value pairs :687
Thread 3 : Hashtable Time for putting key value pairs :2032
Thread 4 : HashMap Time for putting key value pairs :2250
Thread 4 : Hashtable Time for putting key value pairs :250
Thread 5 : HashMap Time for putting key value pairs :359
Thread 5 : Hashtable Time for putting key value pairs :250
Thread 6 : HashMap Time for putting key value pairs :516
Thread 6 : Hashtable Time for putting key value pairs :2937
Thread 7 : HashMap Time for putting key value pairs :312
Thread 7 : Hashtable Time for putting key value pairs :3844
Thread 8 : HashMap Time for putting key value pairs :3657
Thread 8 : Hashtable Time for putting key value pairs :234
Thread 9 : HashMap Time for putting key value pairs :797
Thread 9 : Hashtable Time for putting key value pairs :703
0
 
LVL 28

Assisted Solution

by:rrz
rrz earned 150 total points
ID: 11923884
I played with your code too.  But I am not sure that my code measures what you are interested in measuring.  
>Time for putting key value pairs      
rrz

import java.util.*;
public class HashTestr
{
        public static void main(String s[])
     {
          Hashtable ht = new Hashtable(100000,1);
          HashMap hm = new HashMap(100000,1);
          Integer[] array = new Integer[100000];
          for(int j=0;j<100000;j++){
                 array[j] = new Integer(j);
          }
          for(int k=0;k<10;k++){
                                int i=0;
                                Date start=new Date();
                                for( i=0;i<100000;i++){
                                                       ht.put(array[i],array[i]);
                                }
                                Date end=new Date();
                                System.err.println("Hashtable Time:"
                                                          + (end.getTime()-start.getTime()));
                                start=new Date();
                                for( i=0;i<100000;i++){
                                                       hm.put(array[i],array[i]);
                                }
                                end=new Date();
                                System.err.println("Hashmap Time:"
                                                           + (end.getTime()-start.getTime()));
                                for(int z=0;z<100000;z++){
                                                          array[z] = new Integer(z + k + 1);
                                }
          }
     }
}

/*  output on my machine
Hashtable Time:440
Hashmap Time:710
Hashtable Time:770
Hashmap Time:110
Hashtable Time:50
Hashmap Time:110
Hashtable Time:60
Hashmap Time:110
Hashtable Time:60
Hashmap Time:110
Hashtable Time:110
Hashmap Time:110
Hashtable Time:60
Hashmap Time:110
Hashtable Time:50
Hashmap Time:110
Hashtable Time:110
Hashmap Time:110
Hashtable Time:110
Hashmap Time:110
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11923914
>> for(int z=0;z<100000;z++){
    >>                                                      array[z] = new Integer(z + k + 1);
        >>                        }

I do not understand what is ur intention here
0
 
LVL 28

Expert Comment

by:rrz
ID: 11923974
>what is ur intention here    
I wanted to make sure that the tables actually had to change key and values for each pass through loop.
>new Integer(z + k + 1)    
This way the tables have to change entries in each pass.  I tried many different ways.  But, maybe we still need to do some work to measure your original  query.
>In terms of putting the key/value pair and also accessing them.      
rrz
0
 
LVL 13

Assisted Solution

by:Webstorm
Webstorm earned 75 total points
ID: 11925492
Hi sudhakar_koundinya,

I made a more accurate test program:
  - i don't use Date, but System.currentTimeMillis (faster),
  - i don't use thread to avoid being interrupted,
  - i also measure the time to do the loop, so it only measure the interresting operation.

Here's my test code:


import java.util.*;

public class HashTest
{
    private static HashMap hm=new HashMap();
    private static Hashtable ht=new Hashtable();

    private static final int NBLOOP=100000;

    private static void testPutSame()
    {
        long
            time1=System.currentTimeMillis(),
            time2,
            time3,
            time4;
        for (int i=0;i<NBLOOP;i++);
        time2=System.currentTimeMillis();
        for (int i=0;i<NBLOOP;i++)
            ht.put("AA","BB");
        time3=System.currentTimeMillis();
        for (int i=0;i<NBLOOP;i++)
            hm.put("AA","BB");
        time4=System.currentTimeMillis();
        time4=time4+time1-time3-time2; // == (time4-time3)-(time2-time1)
        time3=time3+time1-2*time2; // == (time3-time2)-(time2-time1)
        System.out.println("Hashtable put same objects : total = "+time3+"ms, each = "+
            (time3/(long)NBLOOP)+"."+(""+((long)NBLOOP+(time3%(long)NBLOOP))).substring(1)+"ms");
        System.out.println("HashMap put same objects : total = "+time4+"ms, each = "+
            (time4/(long)NBLOOP)+"."+(""+((long)NBLOOP+(time4%(long)NBLOOP))).substring(1)+"ms");
    }

    private static void testGetSameExist()
    {
        long
            time1=System.currentTimeMillis(),
            time2,
            time3,
            time4;
        for (int i=0;i<NBLOOP;i++);
        time2=System.currentTimeMillis();
        for (int i=0;i<NBLOOP;i++)
            ht.get("AA");
        time3=System.currentTimeMillis();
        for (int i=0;i<NBLOOP;i++)
            hm.get("AA");
        time4=System.currentTimeMillis();
        time4=time4+time1-time3-time2; // == (time4-time3)-(time2-time1)
        time3=time3+time1-2*time2; // == (time3-time2)-(time2-time1)
        System.out.println("Hashtable get same existing objects : total = "+time3+"ms, each = "+
            (time3/(long)NBLOOP)+"."+(""+((long)NBLOOP+(time3%(long)NBLOOP))).substring(1)+"ms");
        System.out.println("HashMap get same existing objects : total = "+time4+"ms, each = "+
            (time4/(long)NBLOOP)+"."+(""+((long)NBLOOP+(time4%(long)NBLOOP))).substring(1)+"ms");
    }

    private static void testGetSameNotExist()
    {
        long
            time1=System.currentTimeMillis(),
            time2,
            time3,
            time4;
        for (int i=0;i<NBLOOP;i++);
        time2=System.currentTimeMillis();
        for (int i=0;i<NBLOOP;i++)
            ht.get("AC");
        time3=System.currentTimeMillis();
        for (int i=0;i<NBLOOP;i++)
            hm.get("AC");
        time4=System.currentTimeMillis();
        time4=time4+time1-time3-time2; // == (time4-time3)-(time2-time1)
        time3=time3+time1-2*time2; // == (time3-time2)-(time2-time1)
        System.out.println("Hashtable get same not existing objects : total = "+time3+"ms, each = "+
            (time3/(long)NBLOOP)+"."+(""+((long)NBLOOP+(time3%(long)NBLOOP))).substring(1)+"ms");
        System.out.println("HashMap get same not existing objects : total = "+time4+"ms, each = "+
            (time4/(long)NBLOOP)+"."+(""+((long)NBLOOP+(time4%(long)NBLOOP))).substring(1)+"ms");
    }

    private static Object[] obj=new Object[NBLOOP];

    private static void buildObjs()
    {
        System.out.println("Allocating array of objects (1) ...");
        // memory allocation here, not in the loop
        for (int i=0;i<NBLOOP;i++)
            obj[i]="AA"+i;
        System.out.println("Array of objects (1) allocated.");
    }

    private static void buildObjs2()
    {
        System.out.println("Allocating array of objects (2) ...");
        // memory allocation here, not in the loop
        for (int i=0;i<NBLOOP;i++)
            obj[i]="AC"+i;
        System.out.println("Array of objects (2) allocated.");
    }

    private static void testPutDifferent()
    {
        long
            time1=System.currentTimeMillis(),
            time2,
            time3,
            time4;
        for (int i=0;i<NBLOOP;i++);
        time2=System.currentTimeMillis();
        for (int i=0;i<NBLOOP;i++)
            ht.put(obj[i],obj[i]);
        time3=System.currentTimeMillis();
        for (int i=0;i<NBLOOP;i++)
            hm.put(obj[i],obj[i]);
        time4=System.currentTimeMillis();
        time4=time4+time1-time3-time2; // == (time4-time3)-(time2-time1)
        time3=time3+time1-2*time2; // == (time3-time2)-(time2-time1)
        System.out.println("Hashtable put different objects : total = "+time3+"ms, each = "+
            (time3/(long)NBLOOP)+"."+(""+((long)NBLOOP+(time3%(long)NBLOOP))).substring(1)+"ms");
        System.out.println("HashMap put different objects : total = "+time4+"ms, each = "+
            (time4/(long)NBLOOP)+"."+(""+((long)NBLOOP+(time4%(long)NBLOOP))).substring(1)+"ms");
    }

    private static void testGetDifferentExist()
    {
        long
            time1=System.currentTimeMillis(),
            time2,
            time3,
            time4;
        for (int i=0;i<NBLOOP;i++);
        time2=System.currentTimeMillis();
        for (int i=0;i<NBLOOP;i++)
            ht.get(obj[i]);
        time3=System.currentTimeMillis();
        for (int i=0;i<NBLOOP;i++)
            hm.get(obj[i]);
        time4=System.currentTimeMillis();
        time4=time4+time1-time3-time2; // == (time4-time3)-(time2-time1)
        time3=time3+time1-2*time2; // == (time3-time2)-(time2-time1)
        System.out.println("Hashtable get different existing objects : total = "+time3+"ms, each = "+
            (time3/(long)NBLOOP)+"."+(""+((long)NBLOOP+(time3%(long)NBLOOP))).substring(1)+"ms");
        System.out.println("HashMap get different existing objects : total = "+time4+"ms, each = "+
            (time4/(long)NBLOOP)+"."+(""+((long)NBLOOP+(time4%(long)NBLOOP))).substring(1)+"ms");
    }

    private static void testGetDifferentNotExist()
    {
        long
            time1=System.currentTimeMillis(),
            time2,
            time3,
            time4;
        for (int i=0;i<NBLOOP;i++);
        time2=System.currentTimeMillis();
        for (int i=0;i<NBLOOP;i++)
            ht.get(obj[i]);
        time3=System.currentTimeMillis();
        for (int i=0;i<NBLOOP;i++)
            hm.get(obj[i]);
        time4=System.currentTimeMillis();
        time4=time4+time1-time3-time2; // == (time4-time3)-(time2-time1)
        time3=time3+time1-2*time2; // == (time3-time2)-(time2-time1)
        System.out.println("Hashtable get different not existing objects : total = "+time3+"ms, each = "+
            (time3/(long)NBLOOP)+"."+(""+((long)NBLOOP+(time3%(long)NBLOOP))).substring(1)+"ms");
        System.out.println("HashMap get different not existing objects : total = "+time4+"ms, each = "+
            (time4/(long)NBLOOP)+"."+(""+((long)NBLOOP+(time4%(long)NBLOOP))).substring(1)+"ms");
    }

    public static void main(String s[])
    {
        buildObjs();
        testPutSame();
        testPutDifferent();

        testGetSameExist();
        testGetDifferentExist();

        buildObjs2();
        testGetSameNotExist();
        testGetDifferentNotExist();
    }
}



And the output i got with my Intel P3 933MHz (Windows 2000) :

Allocating array of objects (1) ...
Array of objects (1) allocated.
Hashtable put same objects : total = 20ms, each = 0.00020ms
HashMap put same objects : total = 20ms, each = 0.00020ms
Hashtable put different objects : total = 631ms, each = 0.00631ms
HashMap put different objects : total = 230ms, each = 0.00230ms
Hashtable get same existing objects : total = 31ms, each = 0.00031ms
HashMap get same existing objects : total = 10ms, each = 0.00010ms
Hashtable get different existing objects : total = 30ms, each = 0.00030ms
HashMap get different existing objects : total = 40ms, each = 0.00040ms
Allocating array of objects (2) ...
Array of objects (2) allocated.
Hashtable get same not existing objects : total = 0ms, each = 0.00000ms
HashMap get same not existing objects : total = 0ms, each = 0.00000ms
Hashtable get different not existing objects : total = 90ms, each = 0.00090ms
HashMap get different not existing objects : total = 40ms, each = 0.00040ms


It may be interresting to get the results from other machines using my code.
On mine, HashMap is faster than Hashtable.
0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11925788
That are good findings  :-)

After executing ur code, But still I am getting impression that, Hashtable is Fast.
Allocating array of objects (1) ...
Array of objects (1) allocated.
Hashtable put same objects : total = 31ms, each = 0.00031ms
HashMap put same objects : total = 32ms, each = 0.00032ms
Hashtable put different objects : total = 218ms, each = 0.00218ms
HashMap put different objects : total = 344ms, each = 0.00344ms
Hashtable get same existing objects : total = 31ms, each = 0.00031ms
HashMap get same existing objects : total = 16ms, each = 0.00016ms
Hashtable get different existing objects : total = 31ms, each = 0.00031ms
HashMap get different existing objects : total = 63ms, each = 0.00063ms
Allocating array of objects (2) ...
Array of objects (2) allocated.
Hashtable get same not existing objects : total = 16ms, each = 0.00016ms
HashMap get same not existing objects : total = 16ms, each = 0.00016ms
Hashtable get different not existing objects : total = 31ms, each = 0.00031ms
HashMap get different not existing objects : total = 47ms, each = 0.00047ms
0
 
LVL 28

Assisted Solution

by:rrz
rrz earned 150 total points
ID: 11926056
to  Webstorm,
>- i don't use Date, but System.currentTimeMillis (faster),
I didn't see any difference. But I used it anyway.
>- i also measure the time to do the loop
I found the time it takes to do the empty loop, negligible  
Also,  did you take your measurements multiple times ?  
sudhakra and I put ours into a loop ( 10 passes ) because the operating system on our machines might be doing other tasks while testing. Do you have some way of isolating measuring process ?

Here is my latest code with my results below.  I took out the part you questioned because it didn't seem to make a difference. I changed the constructors that I used for Hashtable and HashMap. You can see that the first time that they are filled up there is big difference. Once they are the right size then subsequent measurements are similar.   I don't know what to conclude from all this. What are your thoughts ?        rrz

import java.util.*;
public class HashTestz{
    public static void main(String s[]){
          int i;
          long start,end;
          Object box;
          Hashtable ht = new Hashtable();
          HashMap hm = new HashMap();
          Integer[] array = new Integer[100000];
          for(int j=0;j<100000;j++){
                                     array[j] = new Integer(j);
          }
          for(int k=0;k<10;k++){
                                start = System.currentTimeMillis();
                                for(i=0;i<100000;i++){
                                                       ht.put(array[i],array[i]);
                                }
                                end = System.currentTimeMillis();
                                System.err.print("Hashtable put time:" + (end - start));
                                start = System.currentTimeMillis();
                                for(i=0;i<100000;i++){
                                                       box = ht.get(array[i]);
                                }
                                end = System.currentTimeMillis();
                                System.err.println(" get time:" + (end - start) + " Hashtable");
                                start = System.currentTimeMillis();
                                for(i=0;i<100000;i++){
                                                       hm.put(array[i],array[i]);
                                }
                                end = System.currentTimeMillis();
                                System.err.print("Hashmap put time:" + (end - start));
                                start = System.currentTimeMillis();
                                for(i=0;i<100000;i++){
                                                       box = hm.get(array[i]);
                                }
                                end = System.currentTimeMillis();
                                System.err.println("  get time:" + (end - start) + " HashMap");
          }
    }
}

/*
Hashtable put time:770 get time:50 Hashtable
Hashmap put time:1370  get time:110 HashMap
Hashtable put time:60 get time:50 Hashtable
Hashmap put time:110  get time:60 HashMap
Hashtable put time:110 get time:50 Hashtable
Hashmap put time:60  get time:110 HashMap
Hashtable put time:50 get time:60 Hashtable
Hashmap put time:110  get time:50 HashMap
Hashtable put time:50 get time:60 Hashtable
Hashmap put time:110  get time:50 HashMap
Hashtable put time:60 get time:110 Hashtable
Hashmap put time:50  get time:110 HashMap
Hashtable put time:60 get time:50 Hashtable
Hashmap put time:110  get time:60 HashMap
Hashtable put time:110 get time:50 Hashtable
Hashmap put time:60  get time:110 HashMap
Hashtable put time:50 get time:60 Hashtable
Hashmap put time:110  get time:50 HashMap
Hashtable put time:110 get time:60 Hashtable
Hashmap put time:110  get time:50 HashMap
*/

0
 
LVL 14

Author Comment

by:sudhakar_koundinya
ID: 11940937
I Just gave B because no one of us know correct bench marking performance tests reagrding this.

I am sorry if I hurt you with B and above sentence

Regards
Sudhakar
0
 
LVL 35

Expert Comment

by:girionis
ID: 11940945
So do we know for sure what's going on? I see from the test cases that sometimes HashMap is faster and some other times Hashtable is faster.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 11942790
>> So do we know for sure what's going on? I see from the test cases that sometimes HashMap is faster and some other times Hashtable is faster.

The only explanation i think is that HashMap and Hashtable don't use the same processor instruction set in their native implementation. And these native code may be different (optimized) for each CPU type. On 1 processor, some instructions used by Hashtable are faster than those used by HashMap, and on another processor, instructions used by HashMap may be faster then those used by Hashtable.

In conclusion, we can't choose HashMap or Hashtable in order to have a faster code, if the application will run on different processor types.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question