A class for timing things. : 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.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Development Class » StopWatchScreenshots 
A class for timing things.
        
/* Copyright (C) 2003 Univ. of Massachusetts Amherst, Computer Science Dept.
   This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).
   http://www.cs.umass.edu/~mccallum/mallet
   This software is provided under the terms of the Common Public License,
   version 1.0, as published by http://www.opensource.org.  For further
   information, see the file `LICENSE' included with this distribution. */
//package cc.mallet.util;

/**
 * A class for timing things.
 * Originally inspired by the Timing class in the Stanford NLP cade,
 * but completely rewritten.
 * <p/>
 * Created: Dec 30, 2004
 *
 @author <A HREF="mailto:[email protected]>[email protected]</A>
 @version $Id: Timing.java,v 1.1 2007/10/22 21:37:40 mccallum Exp $
 */
public class Timing {

  private long objCreationTime;
  private long startTime;

  public Timing ()
  {
    startTime = System.currentTimeMillis ();
    objCreationTime = startTime;
  }

  /**
   * Print to System.out how much time has passed, resetting this Timing's start time to
   * the current time.  Time is measured from the most recent
   * <code>tick</code> call, or when this object was created.
   *
   @param msg Prefix of string printed with time
   @return Number of elapsed milliseconds from tick (or start)
   */
  public long tick (String msg)
  {
    long elapsed = report (msg);
    startTime = System.currentTimeMillis ();
    return elapsed;
  }

  /**
   * Returns how much time as passed since Object creation, or the most recent call to tick().
   @return Number of elapsed milliseconds
   */
  public long elapsedTime ()
  {
    return System.currentTimeMillis () - startTime;
  }

  /**
   * Returns the number of milliseconds since this object was created.
   * Ignores previous calls to <tt>tick</tt>, unlike
   * <tt>elapsedTime</tt> and <tt>tick</tt>.
   */
  public long totalElapsedTime ()
  {
    return System.currentTimeMillis () - objCreationTime;
  }

  /** Like tick(), but doesn't reset the counter. */
  public long report (String msg)
  {
    long currentTime = System.currentTimeMillis ();
    long elapsed = currentTime - startTime;
    System.out.println (msg + " time (ms) =  " (elapsed));
    return elapsed;
  }
}

   
    
    
    
    
    
    
    
  
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.Stopwatch is a debugging tool for manually profiling code execution
13.Your own timer
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.