1 11 package org.eclipse.ui.internal.misc; 12 13 import java.util.Map ; 14 import java.util.HashMap ; 15 16 33 public class Stopwatch { 34 private long startTime; 35 private long lastTime; 36 private String name; 37 private static Map registry; 38 42 public Stopwatch(String name) { 43 this.name = name; 44 start(); 45 } 46 49 static public Stopwatch getStopwatch(String name) { 50 if (registry != null) 51 return (Stopwatch)registry.get(name); 52 else 53 return null; 54 } 55 59 public void printInterval(String hint) { 60 long time = System.currentTimeMillis() - lastTime; 61 System.out.println(name + " '" + hint + "' took " + time + " ms"); lastTime = System.currentTimeMillis(); 63 } 64 67 public void printTime() { 68 long time = System.currentTimeMillis() - startTime; 69 System.out.print(name + " is now " + time + " ms");} 71 74 public void register() { 75 if (registry == null) 76 registry = new HashMap (2); 77 registry.put(name, this); 78 } 79 82 public void resetInterval() { 83 lastTime = System.currentTimeMillis(); 84 } 85 88 public void start() { 89 startTime = lastTime = System.currentTimeMillis(); 90 System.out.println(name + " started");} 92 95 public void stop() { 96 long time = System.currentTimeMillis() - startTime; 97 System.out.println(name + " finished in " + time + " ms");} 99 102 public void unregister() { 103 if (registry != null) 104 registry.remove(name); 105 } 106 } 107 | Popular Tags |