SSJ
V. 1.2.5.

umontreal.iro.lecuyer.util
Class Chrono

java.lang.Object
  extended by umontreal.iro.lecuyer.util.Chrono
Direct Known Subclasses:
ChronoSingleThread

public class Chrono
extends Object

Chrono is a small class that acts as an interface to the system clock and calculates the CPU time consumed by parts of a program. Part of this class is implemented in the C language and the implementation is unfortunately operating system-dependent. The C functions for the current class have been compiled on a 32-bit machine running Linux and will not work on 64-bit machines. For a platform-independent CPU timer (valid only with Java-1.5 or later), one should use the subclass ChronoSingleThread which is programmed directly in Java (see the next class in this guide).

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

Below is an example of how it may be used. A stopwatch named timer is constructed (and initialized). When 2.1 seconds of CPU 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.


Chrono timer = Chrono.createForSingleThread();


   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


Constructor Summary
Chrono()
          Constructs a Chrono object and initializes it to zero.
 
Method Summary
static Chrono createForSingleThread()
          Creates a Chrono instance adapted for a program using a single thread.
 String format()
          Converts the CPU time used by the program since its last call to init for this Chrono to a String in the HH:MM:SS.xx format.
static String format(double time)
          Converts the time time, given in seconds, to a String in the HH:MM:SS.xx format.
 double getHours()
          Returns the CPU time in hours used by the program since the last call to init for this Chrono.
 double getMinutes()
          Returns the CPU time in minutes used by the program since the last call to init for this Chrono.
 double getSeconds()
          Returns the CPU time in seconds used by the program since the last call to init for this Chrono.
 void init()
          Initializes this Chrono to zero.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Chrono

public Chrono()
Constructs a Chrono object and initializes it to zero.

Method Detail

createForSingleThread

public static Chrono createForSingleThread()
Creates a Chrono instance adapted for a program using a single thread. Under Java 1.5, this method returns an instance of ChronoSingleThread which can measure CPU time for one thread. Under Java versions prior to 1.5, this returns an instance of this class. This method must not be used to create a timer for a multi-threaded program, because the obtained CPU times will differ depending on the used Java version.

Returns:
the constructed timer.

init

public void init()
Initializes this Chrono to zero.


getSeconds

public double getSeconds()
Returns the CPU time in seconds used by the program since the last call to init for this Chrono.

Returns:
the number of seconds

getMinutes

public double getMinutes()
Returns the CPU time in minutes used by the program since the last call to init for this Chrono.

Returns:
the number of minutes

getHours

public double getHours()
Returns the CPU time in hours used by the program since the last call to init for this Chrono.

Returns:
the number of hours

format

public String format()
Converts the CPU time used by the program since its last call to init for this Chrono to a String in the HH:MM:SS.xx format.

Returns:
the string representation of the CPU time

format

public static String format(double time)
Converts the time time, given in seconds, to a String in the HH:MM:SS.xx format.

Returns:
the string representation of the time time

SSJ
V. 1.2.5.

To submit a bug or ask questions, send an e-mail to Pierre L'Ecuyer.