import java.util.*;
public class Test2
{
static int MAP_SIZE = 100000;
static Integer[] positives = new Integer[MAP_SIZE];
static Integer[] negatives = new Integer[MAP_SIZE];
static
{
for (int i = 1; i < MAP_SIZE; i++)
{
positives[i] = new Integer(i);
negatives[i] = new Integer(-i);
}
}
static void populate(Map m)
{
populate(m, MAP_SIZE);
}
static void populate(Map m, int size)
{
for (int i = 1; i < size; i++)
m.put(positives[i], negatives[i]);
}
static void accessTest(Map m)
{
long start = System.currentTimeMillis();
int total = 0;
for (int i = 1; i < MAP_SIZE; i++)
{
total -= ((Integer) m.get(positives[i])).intValue();
}
long end = System.currentTimeMillis();
System.out.println(total + " get() in time " + (end - start) + " (ms) for class " +
m.getClass().getName());
}
static void containsKeyTest(Map m)
{
long start = System.currentTimeMillis();
int total = 0;
for (int i = 1; i < MAP_SIZE; i++)
{
if (m.containsKey(positives[i]))
total += positives[i].intValue();
}
long end = System.currentTimeMillis();
System.out.println(total + " containsKey() in time " + (end - start) + " (ms) for class
" + m.getClass().getName());
}
static void containsValueTest(Map m)
{
long start = System.currentTimeMillis();
int total = 0;
for (int i = 1; i < MAP_SIZE; i++)
{
if (m.containsValue(negatives[i]))
total += positives[i].intValue();
}
long end = System.currentTimeMillis();
System.out.println(total + " containsValue()/100 in time " + (end - start) + " (ms) for
class " + m.getClass().getName());
}
static void test2(Map m)
{
populate(m);
accessTest(m);
containsKeyTest(m);
containsValueTest(m);
}
public static void main(String[] args)
{
Map m2 = new LinkedHashMap();
test2(m2);
test2(m2);
test2(m2);
Map m1 = new HashMap();
test2(m1);
test2(m1);
test2(m1);
}
}
|