package umontreal.iro.lecuyer.util;

/**
 * AbstractChrono is a class that acts as an interface to
 * the system clock and calculates the CPU or system time consumed by
 * parts of a program.
 * 
 * 

* Every object of class AbstractChrono acts as an independent stopwatch. * Several AbstractChrono objects can run at any given time. * The method {@link #init init} resets the stopwatch to zero, * {@link #getSeconds getSeconds}, {@link #getMinutes getMinutes} and {@link #getHours getHours} * return its current reading, * and {@link #format format} converts this reading to a {@link String}. * The returned value includes the execution time of the method * from AbstractChrono. * *

* Below is an example of how it may be used. * A stopwatch named timer is constructed (and initialized). * When 2.1 seconds of CPU time have been consumed, * the stopwatch is read and reset to zero. * Then, after an additional 330 seconds (or 5.5 minutes) of CPU time, * the stopwatch is read again and the value is printed to the output * in minutes. * *

* * *
* AbstractChrono timer = new Chrono(); *
*
* *
* * *
*
   suppose 2.1 CPU seconds are used here *
*
*
* *
* * double t = timer.getSeconds();               // Here, t = 2.1 *
* timer.init(); *
* t = timer.getSeconds();                      // Here, t = 0.0 *
*
* *
* * *
*
   suppose 330 CPU seconds are used here. *
*
*
* *
* * t = timer.getMinutes();                      // Here, t = 5.5 *
* System.out.println (timer.format());         // Prints: 0:5:30.00 *
*
* */ public abstract class AbstractChrono { private long m_second; private long m_microsec; private long[] now = new long[2]; // tab[0] = seconds, tab[1] = microseconds protected abstract void getTime (long[] tab); public AbstractChrono() { } /** * Initializes this AbstractChrono to zero. * */ public void init() { getTime (now); m_second = now[0]; m_microsec = now[1]; } /** * Returns the CPU time in seconds used by the program since the last call to * {@link #init init} for this AbstractChrono. * * @return the number of seconds * */ public double getSeconds() { getTime (now); double time = (now[1] - m_microsec)/1000000.0 + (now[0] - m_second); return time; } /** * Returns the CPU time in minutes used by the program since the last call to * {@link #init init} for this AbstractChrono. * * @return the number of minutes * */ public double getMinutes() { getTime (now); double time = (now[1] - m_microsec)/1000000.0 + (now[0] - m_second); return time*1.666666667*0.01; } /** * Returns the CPU time in hours used by the program since the last call to * {@link #init init} for this AbstractChrono. * * @return the number of hours * */ public double getHours() { getTime (now); double time = (now[1] - m_microsec)/1000000.0 + (now[0] - m_second); return time*2.777777778*0.0001; } /** * Converts the CPU time used by the program since its last * call to {@link #init init} for this AbstractChrono to a * {@link String} in the HH:MM:SS.xx format. * * @return the string representation of the CPU time * */ public String format() { return format (getSeconds()); } /** * Converts the time time, given in seconds, to a * {@link String} in the HH:MM:SS.xx format. * * @return the string representation of the time time * */ public static String format (double time) { int second, hour, min, centieme; hour = (int)(time/3600.0); if (hour > 0) time -= ((double)hour*3600.0); min = (int)(time/60.0); if (min > 0) time -= ((double)min*60.0); second = (int)time; centieme = (int)(100.0*(time - (double)second) + 0.5); return String.valueOf (hour) + ":" + min + ":" + second + "." + centieme; } }

Ads help maintain this website.