Stopwatch is a debugging tool for manually profiling code execution : StopWatch « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » StopWatch 




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 != )
         return end begin;
      else
         return System.currentTimeMillis() begin;
   }

   public long total() {
      if end != )
         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 > ) {
         sb.append(hour + "h ");
         t = t % 3600000;
      }

      long min = t / 60000;
      if min > ) {
         sb.append(min + "m ");
         t = t % 60000;
      }

      long sec = t / 1000;
      if sec > ) {
         sb.append(sec + "s ");
         t = t % 1000;
      }

      if t > || total() == )
         sb.append(t + "ms");

      return sb.toString();
   }
}

   
    
  














Related examples in the same category
1.StopWatch provides a convenient API for timings.
2.Simulates a stop watch with a lap counter.
3.Provides the programatic analog of a physical stop watch
4.Basic Timer
5.Provides various features related to the current date and timeProvides various features related to the current date and time
6.Simple stop watch
7.Some simple stop watch.
8.Stop Watch with PrintWriter
9.Stop Watch from JGraphT
10.Stop watch with nano second
11.StopWatch reports elapsed time between start and stop
12.Your own timer
13.A class for timing things.
14.A convenience class to do code profiling.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.