import java.util.*;
public class Test1
{
static int MAP_SIZE = 1000000;
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]);
}
public static void main(String[] args)
{
test1();
}
static void test1()
{
Map m = new HashMap();
populate(m);
test1(m);
test1(m);
test1(m);
test1(m);
test2(m);
test2(m);
test2(m);
test2(m);
m = new LinkedHashMap();
populate(m);
test1(m);
test1(m);
test1(m);
test1(m);
test2(m);
test2(m);
test2(m);
test2(m);
}
static void test1(Map m)
{
// System.out.println("Starting");
long start = System.currentTimeMillis();
Object[] keyValuePairs1 = m.entrySet().toArray();
int count = 0;
for(int i = 0; i < keyValuePairs1.length; i++)
{
Map.Entry entry = (Map.Entry) keyValuePairs1[i];
count += ((Integer) entry.getKey()).intValue();
}
long end = System.currentTimeMillis();
System.out.println("Time to count " + count + " using toArray is " + (end-start) + "
(ms) with " + m.getClass().getName());
keyValuePairs1 = null;
System.gc();
try{Thread.sleep(1000);}catch(InterruptedException e){}
System.gc();
try{Thread.sleep(1000);}catch(InterruptedException e){}
// System.out.println("Starting");
start = System.currentTimeMillis();
Iterator keyValuePairs2 = m.entrySet().iterator();
count = 0;
while(keyValuePairs2.hasNext())
{
Map.Entry entry = (Map.Entry) keyValuePairs2.next();
count += ((Integer) entry.getKey()).intValue();
}
end = System.currentTimeMillis();
System.out.println("Time to count " + count + " iterating is " + (end-start) + " (ms)
with " + m.getClass().getName());
keyValuePairs2 = null;
System.gc();
try{Thread.sleep(1000);}catch(InterruptedException e){}
System.gc();
try{Thread.sleep(1000);}catch(InterruptedException e){}
}
static void test2(Map m)
{
// System.out.println("Starting");
Object[] keyValuePairs1 = m.entrySet().toArray();
long start = System.currentTimeMillis();
int count = 0;
for(int i = 0; i < keyValuePairs1.length; i++)
{
Map.Entry entry = (Map.Entry) keyValuePairs1[i];
count += ((Integer) entry.getKey()).intValue();
}
long end = System.currentTimeMillis();
System.out.println("Time to count " + count + " using toArray (ex-creation) is " +
(end-start) + " (ms) with " + m.getClass().getName());
keyValuePairs1 = null;
System.gc();
try{Thread.sleep(1000);}catch(InterruptedException e){}
System.gc();
try{Thread.sleep(1000);}catch(InterruptedException e){}
// System.out.println("Starting");
Iterator keyValuePairs2 = m.entrySet().iterator();
start = System.currentTimeMillis();
count = 0;
while(keyValuePairs2.hasNext())
{
Map.Entry entry = (Map.Entry) keyValuePairs2.next();
count += ((Integer) entry.getKey()).intValue();
}
end = System.currentTimeMillis();
System.out.println("Time to count " + count + " iterating (ex-creation) is " +
(end-start) + " (ms) with " + m.getClass().getName());
keyValuePairs2 = null;
System.gc();
try{Thread.sleep(1000);}catch(InterruptedException e){}
System.gc();
try{Thread.sleep(1000);}catch(InterruptedException e){}
}
}
|