Ben J. Christensen

Software Development and Other Random Stuff

ConcurrentHashMap vs HashMap

A simple test of performance for ConcurrentHashMap.

The insert is only a little slower, but the retrieval is 3x slower in this quick and dirty single-threaded test.

 

TimeUtil timer = new TimeUtil();

        timer.start();

        HashMap map = new HashMap();

        for (int i = 0; i < 1000000; i++) {

            map.put(i, i);

        }

        timer.printCurrent();

        

        timer.start();

        ConcurrentHashMap cmap = new ConcurrentHashMap();

        for (int i = 0; i < 1000000; i++) {

            cmap.put(i, i);

        }

        timer.printCurrent();

        

 

        

        timer = new TimeUtil();

        timer.start();

        map = new HashMap();

        for (int i = 0; i < 1000000; i++) {

            map.get(i);

        }

        timer.printCurrent();

        

        timer.start();

        cmap = new ConcurrentHashMap();

        for (int i = 0; i < 1000000; i++) {

            cmap.get(i);

        }

        timer.printCurrent();

 

Action completed in: 458 ms 0.458 seconds

Action completed in: 574 ms 0.574 seconds

Action completed in: 30 ms 0.03 seconds

Action completed in: 113 ms 0.113 seconds

Filed under: Code, Performance

Leave a Reply

Twitter Updates

  • I *really* wish iBooks and Kindle would let me copy/paste text so I can quote a sentence or paragraph! Ridiculous that I can't. 23 hours ago
  • Great weekend (and ‘food tourism’) in Los Angeles. "Sooo fun!" as described by a certain short person when asked on the drive home. 1 day ago
  • Small world! Just ran into a colleague from work - a 7hr drive from the office! 1 day ago
  • We made it to LA! Feels like returning home :-) 3 days ago
  • Peter Pan Baby http://twitpic.com/2kg6n4 5 days ago
View Ben Christensen's profile on LinkedIn