Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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.

                         

» View all related tutorials
Related Tags: c string reference io method type boolean key rmi ole this ai node oo create if boo example to contains

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.