Stopwatch is a debugging tool for manually profiling code execution
package com.dbxml.util; /* * dbXML - Native XML Database * Copyright (C) 1999-2004 The dbXML Group, L.L.C. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * $Id: Stopwatch.java,v 1.2 2004/02/12 00:17:58 bradford Exp $ */ /** * Stopwatch is a debugging tool for manually profiling code execution. * This class will probably be removed for a production release. */ public final class Stopwatch { private String label; private long total = 0; private long begin = 0; private long end = 0; public Stopwatch() { } public Stopwatch(boolean immediate) { if ( immediate ) start(); } public Stopwatch(String label) { this.label = label; } public Stopwatch(String label, boolean immediate) { this.label = label; if ( immediate ) start(); } public void start() { begin = System.currentTimeMillis(); } public long stop() { end = System.currentTimeMillis(); total += (end - begin); return total; } public void cancel() { begin = 0; end = 0; } public void reset() { total = 0; begin = 0; end = 0; } public long elapsed() { if ( end != 0 ) return end - begin; else return System.currentTimeMillis() - begin; } public long total() { if ( end != 0 ) return total; else return (System.currentTimeMillis() - begin) + total; } public String toString() { return toString(total()); } public String toString(long t) { StringBuffer sb = new StringBuffer(); if ( label != null ) sb.append(label + ": "); long hour = t / 3600000; if ( hour > 0 ) { sb.append(hour + "h "); t = t % 3600000; } long min = t / 60000; if ( min > 0 ) { sb.append(min + "m "); t = t % 60000; } long sec = t / 1000; if ( sec > 0 ) { sb.append(sec + "s "); t = t % 1000; } if ( t > 0 || total() == 0 ) sb.append(t + "ms"); return sb.toString(); } }