2017  Kodetalk | Feedback | Privacy Policy | Terms | About

Performance ConcurrentHashmap vs HashMap

How will be the performance of ConcurrentHashMap compared to HashMap, especially .get() operation (I"m especially interested for the case of only few items, in the range between maybe 0-5000)? Is there any reason not to use ConcurrentHashMap instead of HashMap?

The first difference will be that null values aren"t allowed but just to clarify, obviously the performance in case of actual concurrent access will suffer, but how compares the performance in case of no concurrent access?


Lets see the results of ConcurrentHashMap and HashMap.

I created 6, very basic tests of read/write operations to a different structures. I performed this tests being able to use 6 cores (threads) of my CPU. The tests are:

  • read/write of 6 threads to 6 different HashMaps [graph :HashMap r/w]
  • read/write of 6 threads to 1 ConcurrentHashMap [graph: Concurrent r/w]
  • read/write of 6 threads to 6 different ConcurrentHashMap [graph: Concurrent single r/w]

The results are presented on the graph, Y axis is in seconds.

The summary

  • Reading and writing from ConcurrentHashMap or HashMap with 1 thread access is similarly effective
  • Writing to ConcurrentHashMap by 6 threads is just a little bit slower than writing to separate HashMaps or ConcurrentHashMap
  • Reading from ConcurrentHashMap by 6 threads is slower (about 3x) than reading from single HashMap or ConcurrentHashMap by a single thread. Still the access time is very, very good. It is 0,13s vs 0,3s for reading 1.000.000 entries from a Map.

To sum up, the performance for ConcurrentHashMap is slightly worse when you need to use it from more than one thread but it is not a critical aspect of performance that you should be worried about.

Answer is