Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials
  

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
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. 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.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.