1 package spoon.support.util; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 import java.util.Stack ; 6 7 12 public class Timer { 13 private static List <Timer> timestamps = new ArrayList <Timer>(); 14 15 private static Stack <Timer> current = new Stack <Timer>(); 16 17 23 public static void start(String name) { 24 current.push(new Timer(name)); 25 } 26 27 33 public static void stop(String name) { 34 if (!current.peek().getName().equals(name)) { 35 throw new RuntimeException ("Must stop last timer"); 36 } 37 current.peek().stop(); 38 timestamps.add(current.pop()); 39 } 40 41 44 public static void display() { 45 for (Timer time : timestamps) { 46 System.out.println(time); 47 } 48 } 49 50 String name; 51 52 long start, stop; 53 54 60 public Timer(String name) { 61 super(); 62 this.name = name; 63 start = System.currentTimeMillis(); 64 } 65 66 69 public void stop() { 70 stop = System.currentTimeMillis(); 71 } 72 73 78 public String getName() { 79 return name; 80 } 81 82 85 public long getValue() { 86 return stop - start; 87 } 88 89 92 @Override 93 public String toString() { 94 return getName() + " \t" + getValue() + "ms"; 95 } 96 97 } 98 | Popular Tags |