Last active
August 29, 2015 14:00
-
-
Save andreluisdias/11406231 to your computer and use it in GitHub Desktop.
Simple class to calculate time intervals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.util.Calendar; | |
/** | |
* An utility class to measure time processing. | |
* | |
* @author Andre Luis de Oliveira Dias | |
* @date 29/04/2014. | |
*/ | |
public final class Timer { | |
private long initial = 0L; | |
private String prefix; | |
private static final Logger LOGGER = LoggerFactory.getLogger(Timer.class); | |
public Timer() { | |
super(); | |
this.initial = System.currentTimeMillis(); | |
} | |
public Timer(String prefix) { | |
this(); | |
this.prefix = prefix; | |
} | |
public String stop() { | |
final StringBuilder sb = new StringBuilder(); | |
if (this.prefix != null) { | |
sb.append("[").append(this.prefix).append("]"); | |
} | |
sb.append("Time Elapsed: ").append(this.timeElapsed()); | |
if (LOGGER.isDebugEnabled()) { | |
LOGGER.debug(sb.toString()); | |
} | |
return sb.toString(); | |
} | |
public String timeElapsed() { | |
final StringBuilder sb = new StringBuilder(); | |
final long time = (Calendar.getInstance().getTimeInMillis() - this.initial); | |
sb.append((time / 1000.0)).append(" second(s)."); | |
return sb.toString(); | |
} | |
public static void main (String[] args) throws Exception { | |
Timer c = new Timer(); | |
Timer c1 = new Timer("C1"); | |
Thread.sleep(3000); | |
c.stop(); | |
c1.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment