Saturday, 15th October 2011
Follow WikiJava on twitter now. @Wikijava

Spring Timer

From WikiJava

Jump to: navigation, search




This page is or contains a derivative version of a portion of this GFDL content
The article was originally sourced from Ganesh Gowtham.


In daily life we used to write so much code , ever thought of calculating time taken by each method ?

Rather than writing code in below style

Contents

The code

long startTime = System.currentTimeMillis();
//... line 1 
//... line 2
long endTime = System.currentTimeMillis();
System.out.println("Time Taken by method to execute the lines : "+(endTime-startTime));

Below code which uses the spring frmk to track the time taken by each method execution

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import junit.framework.Assert;
 
import org.springframework.util.StopWatch;
 
/**
 * 
 * @author Ganesh Gowtham
 * http://sites.google.com/site/ganeshgowtham 
 */
public class WatchUtils
{
  private static StopWatch watch = null;
  private static final String  ASSERT_MSG = "StopWatch should be instantiated with resetAllTasks()";
 
  /**
   * creates the Task with name specified
   * @param taskName
   */
  public static void startTask(String taskName)
  {
    Assert.assertNotNull(ASSERT_MSG,watch);
    watch.start(taskName);
  }
 
  /**
   * End's the current task 
   *
   */
  public static void endTask()
  {
    Assert.assertNotNull(ASSERT_MSG,watch);
    watch.stop();
  }
 
  /**
   * prints the summary of Time consumed in formatted manner
   * @return
   */
  public static String getTaskSummary()
  {
    Assert.assertNotNull(ASSERT_MSG,watch);
    return watch.prettyPrint();
  }
  /*
   * reset's all Old task and create the fresh StopWatch
   */
  public static void resetAllTasks()
  {
    watch = new StopWatch("-- DEBUGGING --");
  }
  public static void main(String[] args) {
 
    WatchUtils.resetAllTasks();
    List<Integer> list = new ArrayList<Integer>();
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    WatchUtils.startTask("time taken for adding objects in Arraylist");
    for(int i=0;i<9000;i++){
      list.add(i);
    }
    WatchUtils.endTask();
    WatchUtils.startTask("time taken for adding objects in HashMap");
    for(int i=0;i<9000;i++){
      map.put(i,i);
    }
    WatchUtils.endTask();    
    System.out.println(WatchUtils.getTaskSummary());    
  }
}

Ouput for the above Code .

StopWatch '-- DEBUGGING --': running time (millis) = 29
-----------------------------------------
ms     %     Task name
-----------------------------------------
00005  017%  time taken for adding objects in Arraylist
00024  083%  time taken for adding objects in HashMap

In Above metrics we can see that 83% of time is used by adding elements in Hashmap itself and only 17% is consumed by adding elements in ArrayList.

Practical Usage will be like placing WatchUtils.startTask(" ... ") and WatchUtils.endTask(); before and after the calling methods which executes the logic which you suspect will take some time .


see also


Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Comments on wikijava are disabled now, cause excessive spam.