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
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:
HashMaps[graph :HashMap r/w]
ConcurrentHashMap[graph: Concurrent r/w]
ConcurrentHashMap[graph: Concurrent single r/w]
The results are presented on the graph, Y axis is in seconds.
HashMapwith 1 thread access is similarly effective
ConcurrentHashMapby 6 threads is just a little bit slower than writing to separate
ConcurrentHashMapby 6 threads is slower (about 3x) than reading from single
ConcurrentHashMapby 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.