Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4343436

(coll) HashMap.clear() is slow for empty maps

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 1.3.0
    • core-libs



      Name: skT45625 Date: 06/05/2000


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      HashMap.clear() is unexpectedly slow if called on an empty map. The time taken
      is proportional to the map capacity, regardless of the size of the map. It ought
      to check if the map is already empty in which case it does not need to clear the
      entry table.

      This also affects HashSet().clear() which calls HashMap.clear().

      Here is a sample program that demonstrates the problem (file Test.java):

      import java.util.HashMap;

      public class Test
      {
      public static void main( String[] args )
      {
      for ( int i = 1000; i <= 10000000; i *= 10 )
      {
      HashMap map = new HashMap( i );
      long time = -System.currentTimeMillis();
      map.clear();
      time += System.currentTimeMillis();
      System.out.println( "Time taken to clear map of size "
      + i + ": " + time / 1000.0 + " s" );
      }
      }
      }

      Output on my system (Windows NT 4, 128MB RAM, Pentium III 450MHz):

      Time taken to clear map of size 1000: 0.0 s
      Time taken to clear map of size 10000: 0.0 s
      Time taken to clear map of size 100000: 0.01 s
      Time taken to clear map of size 1000000: 0.02 s
      Time taken to clear map of size 10000000: 0.24 s

      This is particularly a problem if clear() is called repeatedly. I would expect
      that calling clear() on an empty map would be a negligible operation.
      (Review ID: 105633)
      ======================================================================

            smarks Stuart Marks
            skondamasunw Suresh Kondamareddy (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: