2010-12-11 03:35:12 +08:00
|
|
|
package org.broadinstitute.sting.utils;
|
|
|
|
|
|
2011-07-18 08:29:58 +08:00
|
|
|
import com.google.java.contract.Ensures;
|
|
|
|
|
import com.google.java.contract.Invariant;
|
|
|
|
|
import com.google.java.contract.Requires;
|
|
|
|
|
|
2010-12-11 03:35:12 +08:00
|
|
|
import java.io.PrintStream;
|
|
|
|
|
|
|
|
|
|
/**
|
2011-05-23 03:45:31 +08:00
|
|
|
* A useful simple system for timing code. This code is not thread safe!
|
2010-12-11 03:35:12 +08:00
|
|
|
*
|
|
|
|
|
* User: depristo
|
|
|
|
|
* Date: Dec 10, 2010
|
|
|
|
|
* Time: 9:07:44 AM
|
|
|
|
|
*/
|
2011-05-23 03:45:31 +08:00
|
|
|
@Invariant({
|
|
|
|
|
"elapsed >= 0",
|
|
|
|
|
"startTime >= 0",
|
|
|
|
|
"name != null",
|
|
|
|
|
"! running || startTime > 0"})
|
2010-12-11 03:35:12 +08:00
|
|
|
public class SimpleTimer {
|
2011-05-23 03:45:31 +08:00
|
|
|
final private String name;
|
2010-12-11 03:35:12 +08:00
|
|
|
private long elapsed = 0l;
|
2011-05-23 03:45:31 +08:00
|
|
|
private long startTime = 0l;
|
2010-12-11 03:35:12 +08:00
|
|
|
boolean running = false;
|
|
|
|
|
|
2011-05-23 03:45:31 +08:00
|
|
|
/**
|
|
|
|
|
* Creates an anonymous simple timer
|
|
|
|
|
*/
|
|
|
|
|
@Ensures("name != null && name.equals(\"Anonymous\")")
|
|
|
|
|
public SimpleTimer() {
|
|
|
|
|
this("Anonymous");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a simple timer named name
|
|
|
|
|
* @param name of the timer, must not be null
|
|
|
|
|
*/
|
|
|
|
|
@Requires("name != null")
|
|
|
|
|
@Ensures("this.name != null && this.name.equals(name)")
|
2010-12-11 03:35:12 +08:00
|
|
|
public SimpleTimer(String name) {
|
|
|
|
|
this.name = name;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-23 03:45:31 +08:00
|
|
|
/**
|
|
|
|
|
* @return the name associated with this timer
|
|
|
|
|
*/
|
|
|
|
|
@Ensures("result != null")
|
2010-12-11 03:35:12 +08:00
|
|
|
public String getName() {
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-23 03:45:31 +08:00
|
|
|
/**
|
|
|
|
|
* Starts the timer running, and sets the elapsed time to 0. This is equivalent to
|
|
|
|
|
* resetting the time to have no history at all.
|
|
|
|
|
*
|
|
|
|
|
* @return this object, for programming convenience
|
|
|
|
|
*/
|
|
|
|
|
@Requires("running == false")
|
|
|
|
|
@Ensures({"result != null", "elapsed == 0l"})
|
2010-12-11 03:35:12 +08:00
|
|
|
public SimpleTimer start() {
|
|
|
|
|
elapsed = 0l;
|
|
|
|
|
restart();
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-23 03:45:31 +08:00
|
|
|
/**
|
|
|
|
|
* Starts the timer running, without reseting the elapsed time. This function may be
|
|
|
|
|
* called without first calling start(). The only difference between start and restart
|
|
|
|
|
* is that start resets the elapsed time, while restart does not.
|
|
|
|
|
*
|
|
|
|
|
* @return this object, for programming convenience
|
|
|
|
|
*/
|
|
|
|
|
@Requires("running == false")
|
|
|
|
|
@Ensures("result != null")
|
2010-12-11 03:35:12 +08:00
|
|
|
public SimpleTimer restart() {
|
|
|
|
|
running = true;
|
|
|
|
|
startTime = currentTime();
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-23 03:45:31 +08:00
|
|
|
/**
|
|
|
|
|
* @return is this timer running?
|
|
|
|
|
*/
|
|
|
|
|
public boolean isRunning() {
|
|
|
|
|
return running;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return A convenience function to obtain the current time in milliseconds from this timer
|
|
|
|
|
*/
|
2010-12-11 03:35:12 +08:00
|
|
|
public long currentTime() {
|
|
|
|
|
return System.currentTimeMillis();
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-23 03:45:31 +08:00
|
|
|
/**
|
|
|
|
|
* Stops the timer. Increases the elapsed time by difference between start and now. The
|
|
|
|
|
* timer must be running in order to call stop
|
|
|
|
|
*
|
|
|
|
|
* @return this object, for programming convenience
|
|
|
|
|
*/
|
|
|
|
|
@Requires("running == true")
|
|
|
|
|
@Ensures({"result != null", "elapsed >= old(elapsed)", "running == false"})
|
2010-12-11 03:35:12 +08:00
|
|
|
public SimpleTimer stop() {
|
|
|
|
|
running = false;
|
|
|
|
|
elapsed += currentTime() - startTime;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-23 03:45:31 +08:00
|
|
|
/**
|
|
|
|
|
* Returns the total elapsed time of all start/stops of this timer. If the timer is currently
|
|
|
|
|
* running, includes the difference from currentTime() and the start as well
|
|
|
|
|
*
|
|
|
|
|
* @return this time, in seconds
|
|
|
|
|
*/
|
|
|
|
|
@Ensures({
|
|
|
|
|
"result >= (elapsed/1000.0)",
|
|
|
|
|
"result >= 0"})
|
2010-12-11 03:35:12 +08:00
|
|
|
public double getElapsedTime() {
|
2011-05-23 03:45:31 +08:00
|
|
|
return (running ? (currentTime() - startTime + elapsed) : elapsed) / 1000.0;
|
2010-12-11 03:35:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void printElapsedTime(PrintStream out) {
|
|
|
|
|
out.printf("SimpleTimer %s: %.2f%n", name, getElapsedTime());
|
|
|
|
|
}
|
|
|
|
|
}
|