eyt*
June 25, 2006

Clearing an empty java.util.HashMap...

Here is an interesting question; which of the following programs executes faster:

  1. public class ProgramA {
  2.   public static void main( String [] args ) {
  3.     java.util.Map<String, String> map = new java.util.HashMap<String, String>( 100000000 );
  4.     long start = System.currentTimeMillis();
  5.     for ( int i = 0; i < 100; ++ i ) {
  6.       map.clear();
  7.     }
  8.     System.err.println( ( System.currentTimeMillis() - start ) + "ms" );
  9.   }
  10. }
  1. public class ProgramB {
  2.   public static void main( String [] args ) {
  3.     java.util.Map<String, String=> map = new java.util.HashMap<String, String=>( 100000000 );
  4.     long start = System.currentTimeMillis();
  5.     for ( int i = 0; i < 100; ++ i ) {
  6.       if ( ! map.isEmpty() ) {
    // this line is not in the above.
  7.         map.clear();
  8.       }
  9.     }
  10.     System.err.println( ( System.currentTimeMillis() - start ) + "ms" );
  11.   }
  12. }

By simply looking at the code or reading through the documentation, you may think that they are similar since the map is empty, since presumably the clear() method would take this into consideration. But of course, if that were the answer, this would not be much of a blog post.

On my machine, Program_A runs in about 37625 milliseconds with one of my processors pegged whereas the Program_B runs in about 1 millisecond. Why? It would appear that the clear() method recreates its internal representation each time that you call it instead of looking at the current state of the object and realizing that it is indeed empty. Naturally if the initialCapacity is smaller, the program is less affected by this.

Obviously no one writes code exactly like the above snippet, but if you are calling clear() from within a loop, you may want to be aware of this.


Posted 16 years, 1 month ago on June 25, 2006
The trackback url for this post is https://www.eyt.ca/blog/bblog/trackback.php/128/

Comments have now been turned off for this post. If you want to share something, please e-mail me.

Navigation

Recent Posts

eyt*