[Aprog]
[aprog.git] / Aprog / src / net / sourceforge / aprog / tools / TicToc.java
blob20eb5b7899660f715ab453c102837f9bba14466e
1 package net.sourceforge.aprog.tools;
3 import static java.lang.System.currentTimeMillis;
5 /**
6 * Simple time measuring class to manually instrument blocks of code.
7 * <br>Call {@link #tic()} at the beginning and {@link #toc()} at the end.
8 * <br>{@link #getTotalTime()} gives the total time (cumulative) spent in {@link #tic()} {@link #toc()} intervals.
9 *
10 * @author codistmonk (creation 2013-01-25)
12 public final class TicToc {
14 private long totalTime;
16 private long ticTocTime;
18 private long t0;
20 /**
21 * Starts a time interval.
23 * @return The current time in milliseconds
25 public final long tic() {
26 this.totalTime += this.ticTocTime;
27 this.ticTocTime = 0L;
28 return this.t0 = currentTimeMillis();
31 /**
32 * @return The time in milliseconds since the last call to {@link #tic()}
34 public final long toc() {
35 this.ticTocTime = currentTimeMillis() - this.t0;
37 return this.ticTocTime;
40 /**
41 * @return The cumulative time of this timer (sum of tic toc intervals)
43 public final long getTotalTime() {
44 return this.totalTime + this.ticTocTime;