-
Enhancement
-
Resolution: Not an Issue
-
P4
-
None
-
1.3.0
-
generic
-
generic
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)
======================================================================
- relates to
-
JDK-4619094 (coll) add API to request that a collection shrink to reduce unused space
-
- Open
-