FileDocCategorySizeDatePackage
Test2.javaAPI DocExample3269Sun Feb 21 17:17:38 GMT 1999tuning.cache

Test2

public class Test2 extends Object

Fields Summary
static Test2[]
cache_keys
static Object[]
cache_values
static Hashtable
hash
int
cacheRef
int
value
static int
freeIndex
Constructors Summary
public Test2(int i)

    value = i;
  
Methods Summary
public static voidaccess_test()

    Test2 a0 = new Test2(6767676);
    Test2 a1 = new Test2(33);
    Test2 a2 = new Test2(998);
    Test2 a3 = new Test2(3333);
    Test2 a4 = new Test2(12348765);
    Test2 a5 = new Test2(9999);
    Test2 a6 = new Test2(66665);
    Test2 a7 = new Test2(1234);
    Test2 a8 = new Test2(987654);
    Test2 a9 = new Test2(3121219);
    Object o1,o2,o3,o4,o5,o6,o7,o8,o9,o0;
    long time = System.currentTimeMillis();
    for (int i = 0; i < 1000000; i++)
    {
       o1 = plain_access(a0);
       o2 = plain_access(a1);
       o3 = plain_access(a2);
       o4 = plain_access(a3);
       o5 = plain_access(a4);
       o6 = plain_access(a5);
       o7 = plain_access(a6);
       o8 = plain_access(a7);
       o9 = plain_access(a8);
       o0 = plain_access(a9);
    }
    System.out.println("plain access took " + (System.currentTimeMillis()-time));

    time = System.currentTimeMillis();
    for (int i = 0; i < 1000000; i++)
    {
       o1 = cached_access(a0);
       o2 = cached_access(a1);
       o3 = cached_access(a2);
       o4 = cached_access(a3);
       o5 = cached_access(a4);
       o6 = cached_access(a5);
       o7 = cached_access(a6);
       o8 = cached_access(a7);
       o9 = cached_access(a8);
       o0 = cached_access(a9);
    }
    System.out.println("cached access took " + (System.currentTimeMillis()-time));

  
public static java.lang.Objectcached_access(tuning.cache.Test2 i)

    int access = i.cacheRef;
    Object o;
    if (access == -1)
    {
      o = hash.get(i);
      //Wind round to the start of the cache if full up
      if (freeIndex >= cache_keys.length)
        freeIndex = 0;
      //Get the next index;
      access = i.cacheRef = freeIndex++;
      //If there was already something there, set it uncached
      if (cache_keys[access] != null)
      {
        System.out.println("Collsion between " + cache_keys[access] + " and " + i);
        cache_keys[access].cacheRef = -1;
      }
      //And cache our new value.
      cache_keys[access] = i;
      cache_values[access] = o;
      return o;
    }
    else
    {
      return cache_values[access];
    }
  
public booleanequals(java.lang.Object obj)

    if ((obj != null) && (obj instanceof Test2))
	  return value == ((Test2) obj).value;
	else
	  return false;
  
public inthashCode()

    return value;
  
public static voidmain(java.lang.String[] args)


      
  
    try
    {
      System.out.println("started populating");
      populate();
      System.out.println("started accessing");
      access_test();
    }
    catch(Exception e){e.printStackTrace();}
  
public static java.lang.Objectplain_access(tuning.cache.Test2 i)

    return hash.get(i);
  
public static voidpopulate()

    for (int i = 0; i < 100000; i++)
      hash.put(new Test2(i), new Integer(i+5));