Stop Watch with PrintWriter : 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 




Stop Watch with PrintWriter
  
import java.io.PrintWriter;

/**
 * Prints time durations; used for development purposes only.
 * <p>
 * No threads or system resources are harmed in the making of a stop watch. A
 * stop watch simply remembers the start time (and elapsed time when paused) and
 * prints the total &quot;running&quot; time when either {@link #mark} or
 {@link #stop} is called.
 * <p>
 {@link #stop} doesn't really stop anything. You can call stop as many times
 * as you like and it will print the time elapsed since start. It would be easy
 * to change this behavior, but I haven't needed to.
 
 @author Jim Menard, <a href="mailto:[email protected]">[email protected]</a>
 */
public class StopWatch {

  protected String name;

  protected long t0;

  protected long elapsedTime;

  protected PrintWriter out;

  public StopWatch() {
    this(null, null);
  }

  public StopWatch(String name) {
    this(name, null);
  }

  public StopWatch(PrintWriter out) {
    this(null, out);
  }

  public StopWatch(String name, PrintWriter out) {
    this.name = name;
    if (out == null)
      this.out = new PrintWriter(System.err);
    else
      this.out = out;
    elapsedTime = -1L// So we can tell if we are ever started
  }

  /**
   * Remembers the current time and prints a message.
   */
  public void start() {
    start(true);
  }

  /**
   * Remembers the current time and prints a message if requested.
   
   @param printStarting
   *          if <code>true</code> and this stop watch has a name, print a
   *          message
   */
  public void start(boolean printStarting) {
    if (t0 != 0)
      System.err.println("(warning: StopWatch already started; resetting)");
    if (printStarting && name != null)
      System.err.println("starting " + name);
    elapsedTime = 0;
    t0 = System.currentTimeMillis();
  }

  /**
   * Pauses the stop watch.
   */
  public void pause() {
    long now = System.currentTimeMillis();
    elapsedTime += now - t0;
    t0 = 0;
  }

  /**
   * Resumes the stop watch.
   */
  public void resume() {
    t0 = System.currentTimeMillis();
  }

  /**
   * Prints the current elapsed time without stopping.
   */
  public void mark() {
    stop(null, true);
  }

  /**
   * Prints the current elapsed time without stopping, along with the stop watch
   * name if <var>printMark</var> is <code>true</code>.
   
   @param printMark
   *          if <code>true</code>, the stop watch name will be printed
   */
  public void mark(boolean printMark) {
    stop(null, printMark);
  }

  /**
   * Prints the current elapsed time without stopping, along with the stop watch
   * name and <var>msg</var>.
   
   @param msg
   *          a message to print
   */
  public void mark(String msg) {
    stop(msg, true);
  }

  /**
   * Prints the current elapsed time without stopping, along with, along with
   * the stop watch name if <var>printMark</var> is <code>true</code> and the
   * <var>msg</var> if it's not <code>null</code>.
   
   @param msg
   *          a message to print
   @param printMark
   *          if <code>true</code>, the stop watch name will be printed
   */
  public void mark(String msg, boolean printMark) {
    stop(msg, printMark);
  }

  /**
   * Stops the stop watch and prints the name of this stop watch and the current
   * elapsed time.
   */
  public void stop() {
    stop(null, true);
  }

  /**
   * Stops the stop watch and prints the name of this stop watch, <var>msg</var>
   * if non-<code>null</code>, and the current elapsed time.
   
   @param msg
   *          a message to print; may be <code>null</code>
   */
  public void stop(String msg) {
    stop(msg, true);
  }

  /**
   * Prints the current elapsed time, along with the stop watch name if
   * <var>printMark</var> is <code>true</code> and the <var>msg</var> if
   * it's not <code>null</code>.
   
   @param msg
   *          a message to print; may be <code>null</code>
   @param printName
   *          if <code>true</code>, the stop watch name will be printed
   */
  public void stop(String msg, boolean printName) {
    long now = System.currentTimeMillis();

    if (elapsedTime == -1) {
      System.err.println("(StopWatch" (name != null (" \"" + name + '"'"")
          " was stopped without ever being started)");
      return;
    }

    long total = elapsedTime;
    if (t0 != 0)
      total += now - t0;

    String separator = null;
    if (printName && name != null) {
      System.err.print(name);
      separator = ": ";
    }
    if (msg != null) {
      if (separator != null)
        System.err.print(' ');
      System.err.print("(" + msg + ")");
      separator = ": ";
    }
    if (separator != null)
      System.err.print(separator);

    System.err.println("" (total / 1000.0" seconds");
  }

}

   
    
  














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 from JGraphT
9.Stop watch with nano second
10.StopWatch reports elapsed time between start and stop
11.Stopwatch is a debugging tool for manually profiling code execution
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.