From 88509f99bfeba48ad8a69cfa96eac8dead5f4e78 Mon Sep 17 00:00:00 2001 From: iTitus Date: Tue, 1 Dec 2020 17:50:08 +0100 Subject: [PATCH] Use nano time instead of Instant --- .../io/github/ititus/math/time/StopWatch.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/github/ititus/math/time/StopWatch.java b/src/main/java/io/github/ititus/math/time/StopWatch.java index 8879357..fdca0e0 100644 --- a/src/main/java/io/github/ititus/math/time/StopWatch.java +++ b/src/main/java/io/github/ititus/math/time/StopWatch.java @@ -1,15 +1,14 @@ package io.github.ititus.math.time; import java.time.Duration; -import java.time.Instant; public class StopWatch { - private Instant time; + private long time; private boolean running; private StopWatch() { - this.time = null; + this.time = 0; this.running = false; } @@ -26,8 +25,8 @@ public StopWatch start() { throw new IllegalStateException(); } - time = Instant.now(); running = true; + time = System.nanoTime(); return this; } @@ -36,19 +35,13 @@ public Duration stop() { throw new IllegalStateException(); } + long duration = System.nanoTime() - time; running = false; - return Duration.between(time, Instant.now()); + time = 0; + return Duration.ofNanos(duration); } public boolean isRunning() { return running; } - - public Instant getStartTime() { - if (!running) { - throw new IllegalStateException(); - } - - return time; - } }