import java.util.*;
public class Test4
{
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(false, 0.75f);
test1(true, 0.75f);
test1(false, 0.75f);
test1(true, 0.75f);
test1(false, 0.75f);
test1(true, 0.75f);
}
static void test1(boolean primesize, float loadfactor)
{
Map m;
if (primesize)
{
int prime = getPrime((int) (MAP_SIZE/loadfactor) + 1);
System.out.println("(MAP_SIZE*2)+1=" + ((MAP_SIZE*2)+1) + " ((MAP_SIZE/loadfactor) + 1)=" +
(int) ((MAP_SIZE/loadfactor) + 1) + " prime=" + prime);
m = new HashMap(prime, loadfactor);
}
else
{
int prime = getPrime((MAP_SIZE*2)+1);
System.out.println("(MAP_SIZE*2)+1=" + ((MAP_SIZE*2)+1) + " ((MAP_SIZE/loadfactor) + 1)=" +
(int) ((MAP_SIZE/loadfactor) + 1) + " prime=" + prime);
m = new HashMap((MAP_SIZE*2)+1, loadfactor);
}
long start = System.currentTimeMillis();
populate(m);
long end = System.currentTimeMillis();
System.out.println("Time to populate " + (primesize?"with prime":"presized") + " is " +
(end-start) + " (ms) with " + m.getClass().getName());
System.gc();
try{Thread.sleep(1000);}catch(InterruptedException e){}
System.gc();
try{Thread.sleep(1000);}catch(InterruptedException e){}
}
//Stupid brute force prime generator from Glen McCluskey,
//http://java.sun.com/developer/JDCTechTips/2002/tt0806.html
static boolean isPrime(int n) {
// 2 is the smallest prime
if (n <= 2) {
return n == 2;
}
// even numbers other than 2 are not prime
if (n % 2 == 0) {
return false;
}
// check odd divisors from 3
// to the square root of n
for (int i = 3, end = (int)Math.sqrt(n);
i <= end; i += 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
// find the smallest prime >= n
static int getPrime(int n) {
while (!isPrime(n)) {
n++;
}
return n;
}
}
|