Java Timer


While trying to time different implementations of MergeSort (specifically, should MergeSort break down arrays all the way to one item, then merge up, or should it stop at some point and let a less efficient algorithm sort small sets of items - I found that using insertion sort once the size of the list being sorted was less than appx 65 worked best) I put together this class, which replicates your average stopwatch in Java. It has all the basic operations (Start, Stop, Pause, Resume, Reset, and Duration) and makes timing operations a breeze.

Example

public static void main(String[] args){ ArrayList<Integer> list = new ArrayList<Integer>(); for(int i = 0; i < 10000; i++){ list.add((int)(Math.random()*100)); } ArrayList<Integer> list2 = new ArrayList<Integer>(list); Timer t = new Timer(); t.start(); BubbleSort.sort(list); t.lap(); t.stop("BubbleSort took: "); t.start(); MergeSort.sort(list2);t.stop("MergeSort, on the other hand, only took: "); // Will output something like: // BubbleSort took: 4.282 // MergeSort, on the other hand, only took: 0.018 }

Instructions

Simply call whichever static methods you need - this is a pretty self explanatory class.

Questions, comments, or suggestions? Go ahead and Contact Me

Download: Timer.java
Size: 2.05kb