This belongs to Stopwatch
class from Java Princeton Stdlib. This class is meant to measure the time of the execution of an algorithm. The implementation goes like this (removed comments and such data):
public class Stopwatch {
private final long start;
public Stopwatch() {
start = System.currentTimeMillis();
}
public double elapsedTime() {
long now = System.currentTimeMillis();
return (now - start) / 1000.0;
}
}
Since it uses System.currentTimeMillis()
it will work with milliseconds. But the elapsedTime
method already convert it back to seconds but as a double
. So your current formula:
lapTime = (1000 * t1 - 1000 * t0) / 1000;
Is making sure to convert the data into a pure double
operation. So, the formula can be rewritten as:
lapTime = t1 - t0;
With no problem.
Note that still this is not the right way to measure time of execution for Java code. You should use System.nanoTime()
instead.
More info:
Going deeper to understand if there's no difference between these operations, let's create a basic test:
public class FormulaTest {
static double formula1(double t0, double t1) {
return (1000 * t1 - 1000 * t0) / 1000;
}
static double formula2(double t0, double t1) {
return t1 - t0;
}
static void printResults(double t0, double t1) {
System.out.println("t0: " + t0);
System.out.println("t1: " + t1);
System.out.println("Formula1: " + formula1(t0, t1));
System.out.println("Formula2: " + formula1(t0, t1));
System.out.println("---------------------------------------------------");
}
public static void main(String[] args) throws java.lang.Exception {
// your code goes here
printResults(0, 10);
printResults(System.currentTimeMillis(), System.currentTimeMillis());
printResults(System.currentTimeMillis(), System.currentTimeMillis() + 142);
printResults(1.7976931348623157e+300 - 5000, 1.7976931348623157e+300);
printResults(
109999999999999999999999999999999999999999999999999999999999999999999999.0,
119999999999999999999999999999999999999999999999999999999999999999999999.0);
printResults(
10.9999999999999999999999999999999999999999999999999999999999999999999999,
11.9999999999999999999999999999999999999999999999999999999999999999999999);
printResults(
823145321462149234.651985149616914621346234923149621346921394613293423951932415934159213226314,
844329146321496321.532159341563149513495139159341593415793415431951349513891585443951391593151);
printResults(
82314532.1462149234651985149616914621346234923149621346921394613293423951932415934159213226314,
84432914.6321496321532159341563149513495139159341593415793415431951349513891585443951391593151);
printResults(1.7976931348623157e+307, 4.9e-323);
printResults(Double.MAX_VALUE, Double.MAX_VALUE);
printResults(Double.MIN_VALUE - 10, Double.MIN_VALUE);
}
}
Output:
t0: 0.0
t1: 10.0
Formula1: 10.0
Formula2: 10.0
---------------------------------------------------
t0: 1.410290897577E12
t1: 1.410290897577E12
Formula1: 0.0
Formula2: 0.0
---------------------------------------------------
t0: 1.410290897577E12
t1: 1.410290897719E12
Formula1: 142.0
Formula2: 142.0
---------------------------------------------------
t0: 1.7976931348623156E300
t1: 1.7976931348623156E300
Formula1: 0.0
Formula2: 0.0
---------------------------------------------------
t0: 1.1E71
t1: 1.2E71
Formula1: 9.999999999999985E69
Formula2: 9.999999999999985E69
---------------------------------------------------
t0: 11.0
t1: 12.0
Formula1: 1.0
Formula2: 1.0
---------------------------------------------------
t0: 8.2314532146214925E17
t1: 8.4432914632149632E17
Formula1: 2.1183824859347024E16
Formula2: 2.1183824859347024E16
---------------------------------------------------
t0: 8.231453214621492E7
t1: 8.443291463214964E7
Formula1: 2118382.4859347227
Formula2: 2118382.4859347227
---------------------------------------------------
t0: 1.7976931348623158E307
t1: 4.9E-323
Formula1: -Infinity
Formula2: -Infinity
---------------------------------------------------
t0: 1.7976931348623157E308
t1: 1.7976931348623157E308
Formula1: NaN
Formula2: NaN
---------------------------------------------------
t0: -10.0
t1: 4.9E-324
Formula1: 10.0
Formula2: 10.0
---------------------------------------------------
Not even at edge cases Double.MAX_VALUE
and Double.MIN_VALUE
are differences between the results of the formula. Even when the result is -Infinity
or NaN
(not a number).
In short: use the last formula:
lapTime = t1 - t0;