Skip to content

Instantly share code, notes, and snippets.

@Koboo
Created August 22, 2022 09:28
Show Gist options
  • Save Koboo/9f54a28bedf7994a18f0e0ee23f0b95b to your computer and use it in GitHub Desktop.
Save Koboo/9f54a28bedf7994a18f0e0ee23f0b95b to your computer and use it in GitHub Desktop.
Simple TmeStamp class to measure times
import java.util.concurrent.TimeUnit;
public class TimeStamp {
private static final String ZERO = "0";
private static final String MILLIS = "ms";
private static final String NANOS = "ns";
private static final String SECONDS = "s";
private static final String ZERO_MILLIS = ZERO + MILLIS;
private static final String ZERO_NANOS = ZERO + NANOS;
private static final String ZERO_SECONDS = ZERO + SECONDS;
private Long start;
private Long end;
public TimeStamp() {
}
public TimeStamp start() {
start = System.nanoTime();
return this;
}
public TimeStamp end() {
end = System.nanoTime();
return this;
}
public String nanos() {
if(start == null || end == null) {
return ZERO_NANOS;
}
long result = end - start;
if(result < 0) {
return ZERO_NANOS;
}
return result + NANOS;
}
public String millis() {
if(start == null || end == null) {
return ZERO_MILLIS;
}
long result = end - start;
if(result < 0) {
return ZERO_MILLIS;
}
return TimeUnit.NANOSECONDS.toMillis(result) + MILLIS;
}
public String seconds() {
if(start == null || end == null) {
return ZERO_SECONDS;
}
long result = end - start;
if(result < 0) {
return ZERO_SECONDS;
}
return TimeUnit.SECONDS.toMillis(result) + SECONDS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment