A class that allows a programmer to determine the amount of time spend doing certain routines : Timer « 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 » Timer 




A class that allows a programmer to determine the amount of time spend doing certain routines
      
/**
 * Profiler.java Created Aug 14, 2006 by Andrew Butler, PSL
 */
//package prisms.util;

/**
 * A class that allows a programmer to determine the amount of time spend doing
 * certain routines
 */
public final class Profiler {
  /**
   * The static instance of Profiler, for ease of use
   */
  public static final Profiler P = new Profiler("Main");

  private String theName;

  private String[] theRoutines;

  private long[] theRoutineStartTimes;

  private long[] theRoutineProfiles;

  private int[] theCallCounts;

  /**
   * Creates a profiler
   
   @param name
   *            The name for the profiler
   */
  public Profiler(String name) {
    theName = name;
    clearSession();
  }

  /**
   * Starts a profiling session
   */
  public void startSession() {
    if (theRoutineStartTimes[0>= 0)
      throw new IllegalStateException("Profiler session \"" + theName
          "\" is already running");
    theRoutineStartTimes[0= System.currentTimeMillis();
  }

  /**
   * Stops the profiling session
   */
  public void stopSession() {
    long time = System.currentTimeMillis();
    for (int i = 0; i < theRoutines.length; i++) {
      if (theRoutineStartTimes[i0)
        continue;
      theRoutineProfiles[i+= time - theRoutineStartTimes[i];
      theRoutineStartTimes[i= -1;
    }
  }

  /**
   * Removes all data from this Profiler, effectively starting over
   */
  public void clearSession() {
    theRoutines = new String[] { "Other" };
    theRoutineStartTimes = new long[] { -};
    theRoutineProfiles = new long[] { };
    theCallCounts = new int[] { };
  }

  /**
   @return A string report showing the amount of time spent in the various
   *         routines since the session was cleared
   */
  public String printReport() {
    StringBuffer ret = new StringBuffer("Profile \"" + theName
        "\" Report:\n");
    int i;
    for (i = 1; i < theRoutines.length; i++) {
      ret.append(printProfile(i));
      ret.append('\n');
    }
    ret.append(printProfile(0));
    ret.append('\n');
    return ret.toString();
  }

  private String printProfile(int i) {
    StringBuffer ret = new StringBuffer(theRoutines[i]);
    ret.append(": ");
    if (i > 0) {
      ret.append(theCallCounts[i]);
      ret.append(" calls: ");
    }
    int days, hrs, mins, secs, millis;
    long profile = theRoutineProfiles[i];
    millis = (int) (profile % 1000);
    profile /= 1000;
    secs = (int) (profile % 60);
    profile /= 60;
    mins = (int) (profile % 60);
    profile /= 60;
    hrs = (int) (profile % 24);
    profile /= 24;
    days = (intprofile;
    if (days > 0) {
      ret.append(days);
      ret.append(" days ");
    }
    if (hrs > 0) {
      ret.append(hrs);
      ret.append(" hours ");
    }
    if (mins > 0) {
      ret.append(mins);
      ret.append(" minutes ");
    }
    if (secs > 0) {
      ret.append(secs);
      ret.append(" seconds ");
    }
    if (millis > 0) {
      ret.append(millis);
      ret.append(" millis");
    }
    return ret.toString();
  }

  /**
   * Notifies the profiler that a routine has started
   
   @param name
   *            The name of the routine
   */
  public void startRoutine(String name) {
    name = name.intern();
    int i;
    for (i = 1; i < theRoutines.length && name != theRoutines[i]; i++)
      ;
    if (i == theRoutines.length) {
      String[] newRoutines = new String[i + 1];
      System.arraycopy(theRoutines, 0, newRoutines, 0, i);
      newRoutines[i= name;
      theRoutines = newRoutines;
      long[] longArray = new long[i + 1];
      System.arraycopy(theRoutineStartTimes, 0, longArray, 0, i);
      longArray[i= -1;
      theRoutineStartTimes = longArray;
      longArray = new long[i + 1];
      System.arraycopy(theRoutineProfiles, 0, longArray, 0, i);
      longArray[i0;
      theRoutineProfiles = longArray;
      int[] intArray = new int[i + 1];
      System.arraycopy(theCallCounts, 0, intArray, 0, i);
      intArray[i0;
      theCallCounts = intArray;
    }
    if (theRoutineStartTimes[i>= 0) {
      stopRoutine(name);
      // throw new IllegalStateException("Routine " + name
      // + " already running: recursion not supported");
    }
    long time = System.currentTimeMillis();
    theRoutineStartTimes[i= time;
    theCallCounts[i]++;
    if (theRoutineStartTimes[0>= 0) { // Stop the "other" routine
      theRoutineProfiles[0+= time - theRoutineStartTimes[0];
      theRoutineStartTimes[0= -1;
    }
  }

  /**
   * Notifies the profiler that a routine has ended
   
   @param name
   *            The name of the routine
   */
  public void stopRoutine(String name) {
    name = name.intern();
    int i;
    for (i = 1; i < theRoutines.length && name != theRoutines[i]; i++)
      ;
    if (i == theRoutines.length || theRoutineStartTimes[i0)
      throw new IllegalStateException("Routine " + name
          " is already finished or has not been started");
    long time = System.currentTimeMillis();
    theRoutineProfiles[i+= time - theRoutineStartTimes[i];
    theRoutineStartTimes[i= -1;
    for (i = 1; i < theRoutines.length && theRoutineStartTimes[i0; i++)
      ;
    if (i == theRoutines.length)
      theRoutineStartTimes[0= time;
  }
}

   
    
    
    
    
    
  














Related examples in the same category
1.Timer Skipping BeepTimer Skipping Beep
2.Timer Schedule a task that executes once every secondTimer Schedule a task that executes once every second
3.Use java.util.Timer to schedule a task to execute once 5 seconds have passedUse java.util.Timer to schedule a task to execute once 5 seconds have passed
4.Timer utilities
5.Timer and TimerTask Classes
6.Pause and start a timer task
7.Create a Timer object
8.Swing also provide a Timer class. A Timer object will send an ActionEvent to the registered ActionListener.
9.Schedule a task by using Timer and TimerTask.
10.Scheduling a Timer Task to Run Repeatedly
11.Create a scheduled task using timer
12.extends TimerTask to create your own task
13.A simple implementation of the Java 1.3 java.util.Timer API
14.Scheduling a Timer Task to Run at a Certain Time
15.Class encapsulating timer functionality
16.Timeout Observer
17.A pool of objects that should only be used by one thread at a time
18.All times in this Timer object are in milliseconds. The timer starts from the moment of it's creation.
19.Thread Timer
20.Cancel Aware Timer
21.HRTimer is a simple system-wide timer facility using a singleton Timer, with additional instrumentation.
22.Used for timing events with millisecond precision.
23.A class to wait until a condition becomes true.
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.