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.


 

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

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

Share on Google+Share on Google+

Decreasing process time by caching through the Hash Table in Java

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

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:1
Pooja
April 25, 2011
Caching

Learn search algorithms to get object from cache

Ads

 

Ads