Java/Spring/Utilities
Using StopWatch (demo from ganesh.gowtham at valtech.co.in)
<source lang="java">
//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.ru * ganesh.gowtham@gmail.ru */
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()); }
}
</source>