Using StopWatch (demo from ganesh.gowtham at valtech.co.in) : Utilities « Spring « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Spring » UtilitiesScreenshots 
Using StopWatch (demo from ganesh.gowtham at valtech.co.in)

//Ganesh Gowtham (ganesh.gowtham@valtech.co.in)       
       
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 blrsgga (Ganesh Gowtham)
 * http:\\ganesh.gowtham.googlepages.com
 * ganesh.gowtham@gmail.com
 */
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());    
  }
}



           
       
Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.