Home Java Example Java Util Decreasing process time by caching through the Hash Table in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Decreasing process time by caching through the Hash Table in Java

Advertisement
This section illustrates you how to improve the process time of any type of the operation performed by your java program.

Decreasing process time by caching through the Hash Table in Java

     

This section illustrates you how to improve the process time of any type of the operation performed by your java program. You can easily improve the process completion time of the operation by using more and more cache of your system. Here, an example with complete code of the program is provided for best illustration of the varying operation in plain way and completing operation by caching and show the whole completion time of the process of the operation. After all you can see and decide that the process time of the operation is more less which is completed by using the caching method, than the process time of the operation, which is completed in plain way. Following program gives you the process time in millisecond.

Code Description:

Object[] cache_values = new Object[128]:
Above code creates a new Object type array "cache_values" size of 128, which is used for caching for in the completion of the operation. This is helpful in completing the process of the operation by caching method that takes less time period.

HashTable.get(Object key):
This is the method of the HashTable class, which returns the value existed in the hash table corresponding to the given key. This method is used for getting values from the hash table in a simple way.

Here is the code of the program:

import java.util.*;

public class DecreasingProcessTime{
  static DecreasingProcessTime[]

 
cache_keys = new DecreasingProcessTime[128];
  static Object[] cache_values = new Object[128];
  static Hashtable<Object,Object> hash = new Hashtable<Object,Object>();
  static int freeIndex = 0;
  int cacheRef = -1;
  int value;
  public static void main(String[] args){
  try{
  System.out.println("started populating");
  populate();
  System.out.println("started accessing");
  access_DecreasingProcessTime();
  }
  catch(Exception e){
  e.printStackTrace();
  }
  }
  public DecreasingProcessTime(int i){
  value = i;
  }
  public int hashCode(){
  return value;
  }
  public boolean equals(Object obj){
  if((obj != null&& (obj instanceof DecreasingProcessTime))
  return value == ((DecreasingProcessTimeobj).value;
  else
  return false;
  }
  public static void populate(){
  for (int i = 0; i < 100000; i++)
  hash.put(new DecreasingProcessTime(i)new Integer(i+5));
  }
  public static Object plain_access(DecreasingProcessTime i){
  return hash.get(i);
  }
  public static Object cached_access(DecreasingProcessTime i){
  int access = i.cacheRef;
  Object o;
  if(access == -1){
  o = hash.get(i);
  if(freeIndex >= cache_keys.length)
  freeIndex = 0;
  access = i.cacheRef = freeIndex++;
  if (cache_keys[access!= null){
  System.out.println("Collsion between " + cache_keys[access]
 
" and " + i);
  cache_keys[access].cacheRef = -1;
  }
  cache_keys[access= i;
  cache_values[access= o;
  return o;
  }
  else{
  return cache_values[access];
  }
  }
  
  public static void access_DecreasingProcessTime(){
  DecreasingProcessTime a0 = new DecreasingProcessTime(6767676);
  DecreasingProcessTime a1 = new DecreasingProcessTime(33);
  DecreasingProcessTime a2 = new DecreasingProcessTime(998);
  DecreasingProcessTime a3 = new DecreasingProcessTime(3333);
  DecreasingProcessTime a4 = new DecreasingProcessTime(12348765);
  DecreasingProcessTime a5 = new DecreasingProcessTime(9999);
  DecreasingProcessTime a6 = new DecreasingProcessTime(66665);
  DecreasingProcessTime a7 = new DecreasingProcessTime(1234);
  DecreasingProcessTime a8 = new DecreasingProcessTime(987654);
  DecreasingProcessTime a9 = new DecreasingProcessTime(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));
  }
}

Download this example.

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 16, 2007

Ask Questions?    Discuss: Decreasing process time by caching through the Hash Table in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Pooja
April 25, 2011
Caching

Learn search algorithms to get object from cache
DMCA.com