Replace System.currentTimeMillis() by System.nanoTime()

- System.nanoTime() is the best way to measure elapsed time in Java.
   - It gives a resolution on the order of microseconds

The System.currentTimeMillis() is used when calculating absolut time.

Signed-off-by: wilderrodrigues <wrodrigues@schubergphilis.com>
This commit is contained in:
wilderrodrigues 2015-06-22 14:07:33 +02:00
parent c0a1009740
commit 78c802a539

View File

@ -23,24 +23,20 @@ public class Profiler {
private Long startTickInMs;
private Long stopTickInMs;
public Profiler() {
startTickInMs = null;
stopTickInMs = null;
}
public long start() {
startTickInMs = System.currentTimeMillis();
startTickInMs = System.nanoTime();
return startTickInMs.longValue();
}
public long stop() {
stopTickInMs = System.currentTimeMillis();
stopTickInMs = System.nanoTime();
return stopTickInMs.longValue();
}
public long getDuration() {
if (startTickInMs != null && stopTickInMs != null)
if (startTickInMs != null && stopTickInMs != null) {
return stopTickInMs.longValue() - startTickInMs.longValue();
}
return -1;
}
@ -55,12 +51,14 @@ public class Profiler {
@Override
public String toString() {
if (startTickInMs == null)
if (startTickInMs == null) {
return "Not Started";
}
if (stopTickInMs == null)
if (stopTickInMs == null) {
return "Started but not stopped";
}
return "Done. Duration: " + getDuration() + "ms";
}
}
}