Home arrow static arrow Java Programming [Archive] - how is the performance for HashMap.get()
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - how is the performance for HashMap.get()
This topic has 9 replies on 1 page.

Posts:781
Registered: 5/1/01
how is the performance for HashMap.get()  
Jun 16, 2004 10:03 AM



 
hi all
i just want to ask you guys how quick the hashmap.get() runs for a given key, if there are thousand of entries in the hashmap. thanks
 

Posts:13,769
Registered: 00-11-29
Re: how is the performance for HashMap.get()  
Jun 16, 2004 10:08 AM (reply 1 of 9)



 
Very fast. It is an O(1) (constant time) method. The number of entries does not matter.

Somethings that will affect it are: the speed of the hashCode method of the object passed to get, the load factor, and how distributed the hashcodes are.
 

Posts:781
Registered: 5/1/01
Re: how is the performance for HashMap.get()  
Jun 16, 2004 10:16 AM (reply 2 of 9)



 
thanks for ur comments, both key and value are String type. so i guess it should affect much, right?
 

Posts:13,769
Registered: 00-11-29
Re: how is the performance for HashMap.get()  
Jun 16, 2004 10:19 AM (reply 3 of 9)



 
thanks for ur comments, both key and value are String
type. so i guess it should affect much, right?

It should be fast. How long are the Strings? I think somone said that String now uses all it's chars to build the hashcode.
 

Posts:781
Registered: 5/1/01
Re: how is the performance for HashMap.get()  
Jun 16, 2004 10:41 AM (reply 4 of 9)



 
it could be as long as 25 chars.
 

Posts:13,769
Registered: 00-11-29
Re: how is the performance for HashMap.get()  
Jun 16, 2004 10:45 AM (reply 5 of 9)



 
it could be as long as 25 chars.

No problem dude. Yer golden.
 

Posts:1,183
Registered: 1/23/02
Re: how is the performance for HashMap.get()  
Jun 16, 2004 10:47 AM (reply 6 of 9)



 
It is an O(1) (constant time) method ... and how distributed the hashcodes are
public class Banana {    public int hashCode() {        return 0; // Eat this, suckaaaaa!    }}

:-)
 

Posts:37,103
Registered: 3/30/99
Re: how is the performance for HashMap.get()  
Jun 16, 2004 10:49 AM (reply 7 of 9)



 
If you want to know how something is going to perform, write a tiny program and test it. Either put it on a profiler, or just put time markers around a big loop. Here's something that somebody (UJ, I think) came up with recently for comparing casting vs. toString after extracting from a Map. It should be easy to adapt to just the Map.get() timing. In fact, I think I added a chunk to do just that, as a control. Knock yourself out.
import java.util.*; public class MapTiming {    Random rnd = new Random(13L);     public static void main(String[] args) {        int numEntries = Integer.parseInt(args[0]);        Integer key = Integer.valueOf(args[1]);         new MapTiming().zillion2(numEntries, key);    }     void zillion2(int numEntries, Integer key) {        HashMap map = new HashMap();        int ix;        for (ix = 0; ix < numEntries; ix++) {            int entry = rnd.nextInt();            map.put(new Integer(entry), String.valueOf(ix));        }        map.put(key, String.valueOf(ix));        final int LOOPS = 10000000;        final int TIMES = 5;        long time;        //        for (int j=0; j<TIMES; j++) {            time = System.currentTimeMillis();            for (int i=0;i<LOOPS; i++) {                String item = map.get(key).toString();            }            System.out.println("toString= " + (System.currentTimeMillis()-time));        }        //        for (int j=0; j<TIMES; j++) {            time = System.currentTimeMillis();            for (int i=0;i<LOOPS; i++) {                String item = (String)map.get(key);            }            System.out.println("cast= " + (System.currentTimeMillis()-time));        }        //        for (int j=0; j<TIMES; j++) {            time = System.currentTimeMillis();            for (int i=0;i<LOOPS; i++) {                Object obj = map.get(key);            }            System.out.println("get= " + (System.currentTimeMillis()-time));        }        //        for (int j=0; j<TIMES; j++) {            time = System.currentTimeMillis();            for (int i=0;i<LOOPS; i++) {                String item = map.get(key).toString();            }            System.out.println("toString= " + (System.currentTimeMillis()-time));        }        //        for (int j=0; j<TIMES; j++) {            time = System.currentTimeMillis();            for (int i=0;i<LOOPS; i++) {                String item = (String)map.get(key);            }            System.out.println("cast= " + (System.currentTimeMillis()-time));        }        //        for (int j=0; j<TIMES; j++) {            time = System.currentTimeMillis();            for (int i=0;i<LOOPS; i++) {                Object obj = map.get(key);            }            System.out.println("get= " + (System.currentTimeMillis()-time));        }    }} 
 

Posts:13,769
Registered: 00-11-29
Re: how is the performance for HashMap.get()  
Jun 16, 2004 10:53 AM (reply 8 of 9)



 
Why doesn't anyone ever use my extensible time tester framework? I've posted it a bunch of times.
 

Posts:13,769
Registered: 00-11-29
Re: how is the performance for HashMap.get()  
Jun 16, 2004 10:56 AM (reply 9 of 9)



 
http://forum.java.sun.com/thread.jsp?forum=426&thread=502816&start=15&range=15&hilite=false&q=
 
This topic has 9 replies on 1 page.