[Fertilizer] toward calculate the time difference, you may not be the best way to practice!

We calculate the time difference between the two codes, many students the following company code is adopted in this way.

long startTime = System.currentTimeMillis();
// 执行代码
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
复制代码

First to explain that this is not a way not. In accordance with the principle of "run on the line," the code is definitely usable! But this is not the best practice, why?

We first look at the comments in the JDK

/**
 * Returns the current time in milliseconds.  Note that
 * while the unit of time of the return value is a millisecond,
 * the granularity of the value depends on the underlying
 * operating system and may be larger.  For example, many
 * operating systems measure time in units of tens of
 * milliseconds.
 *
 * <p> See the description of the class <code>Date</code> for
 * a discussion of slight discrepancies that may arise between
 * "computer time" and coordinated universal time (UTC).
 *
 * @return  the difference, measured in milliseconds, between
 *          the current time and midnight, January 1, 1970 UTC.
 * @see     java.util.Date
 */
public static native long currentTimeMillis();
复制代码

Of course, some students, after all, limited English proficiency, that fat can only be used toward the introduction of artifacts document before coming straight to the point

Although the text of the understanding every word, but these words want to say you may still not very clear, we have a saying, the reality is not afraid, afraid of comparison. Then we look the other way.

long startTime = System.nanoTime();
// 执行代码
long endTime = System.nanoTime();
System.out.println(endTime - startTime);
复制代码

Similarly, we look at comments

/**
 * Returns the current value of the running Java Virtual Machine's
 * high-resolution time source, in nanoseconds.
 *
 * <p>This method can only be used to measure elapsed time and is
 * not related to any other notion of system or wall-clock time.
 * The value returned represents nanoseconds since some fixed but
 * arbitrary <i>origin</i> time (perhaps in the future, so values
 * may be negative).  The same origin is used by all invocations of
 * this method in an instance of a Java virtual machine; other
 * virtual machine instances are likely to use a different origin.
 *
 * <p>This method provides nanosecond precision, but not necessarily
 * nanosecond resolution (that is, how frequently the value changes)
 * - no guarantees are made except that the resolution is at least as
 * good as that of {@link #currentTimeMillis()}.
 *
 * <p>Differences in successive calls that span greater than
 * approximately 292 years (2<sup>63</sup> nanoseconds) will not
 * correctly compute elapsed time due to numerical overflow.
 *
 * <p>The values returned by this method become meaningful only when
 * the difference between two such values, obtained within the same
 * instance of a Java virtual machine, is computed.
 *
 * <p> For example, to measure how long some code takes to execute:
 *  <pre> {@code
 * long startTime = System.nanoTime();
 * // ... the code being measured ...
 * long estimatedTime = System.nanoTime() - startTime;}</pre>
 *
 * <p>To compare two nanoTime values
 *  <pre> {@code
 * long t0 = System.nanoTime();
 * ...
 * long t1 = System.nanoTime();}</pre>
 *
 * one should use {@code t1 - t0 < 0}, not {@code t1 < t0},
 * because of the possibility of numerical overflow.
 *
 * @return the current value of the running Java Virtual Machine's
 *         high-resolution time source, in nanoseconds
 * @since 1.5
 */
public static native long nanoTime();
复制代码

At this time, we'll come up with Artifact:

From the text of the description plus sample code, told us:

Again, the reality is not afraid, afraid of comparison. Even if you know nothing about the code, according to the primary language understanding, it can be seen in the implementation of the code to calculate the time difference, which way the election, is the best practice.

Finally, let's look at Alibaba development manual for our recommendations:

Your company code is how to calculate the time difference, please leave a message to tell fat towards.


Guess you like

Origin juejin.im/post/5d1aab646fb9a07f0870adf4