Java Timer

About

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

// Timing the difference between BubbleSort and MergeSort ArrayList list = new ArrayList(); // fill ArrayList with a million values ArrayList list2 = list.clone(); Timer.start(); Sort.bubbleSort(list); Timer.stop("BubbleSort took: "); Timer.start(); Sort.mergeSort(list2); Timer.stop("MergeSort, on the other hand, only took: "); // Will output something like: // BubbleSort took: 16572.158 // MergeSort, on the other hand, only took: 11.248

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: Zip Archive
Size: 1.02kb