0

I tried to make a method that will give me an idea of how much time it takes to my programs to run, and for an unknown reason, I can not get the nanoseconds.

I tried to read about the below classes but could not find out what I am doing wrong: import java.time.Duration; import java.time.Instant;

The methods:

public static String calcExecutionTime(Instant startTime, Instant endTime) {
    if ((Duration.between(startTime, endTime).toNanos()) < 1000000) {
        return Duration.between(startTime, endTime).toNanos() + " nanoseconds.";
    } else if ((Duration.between(startTime, endTime).toMillis()) < 1000) {
        return Duration.between(startTime, endTime).toMillis() + " milliseconds.";
    } else if ((Duration.between(startTime, endTime).toSeconds()) < 100) {
        return Duration.between(startTime, endTime).toSeconds() + " seconds.";
    } else if ((Duration.between(startTime, endTime).toMinutes()) < 60) {
        return Duration.between(startTime, endTime).toMinutes() + " minutes.";
    } else {
        return Duration.between(startTime, endTime).toHours() + " hoursm.";
    }
}

Main here:

    Instant instantStart = Instant.now();
    Instant instantEnd = Instant.now();
    System.out.print("Execution time for this program: " + calcExecutionTime(instantStart, instantEnd));

If I use the old System.nanoTime(); I get a result of 300-400 nanoseconds but with toNanos() I get 0 in this same situation.

Edit: I do not use getNano() I use toNanos()

David
  • 145
  • 1
  • 3
  • 13
  • 3
    `Duration#toNanos()` works fine, it is `Instant.now()` which gives you twice same time causing duration to be 0. – Pshemo Oct 07 '19 at 10:29
  • 1
    Hint: print out the `Instant` instances and `Duration.between(startTime, endTime)` – user85421 Oct 07 '19 at 10:29
  • Maybe your system does not provide this information. As mentioned already log both values and additional see this question and answer https://stackoverflow.com/questions/20689055/java-8-instant-now-with-nanosecond-resolution#20689231 – SubOptimal Oct 07 '19 at 10:36
  • 2
    Possible duplicate of [Java 8 Instant.now() with nanosecond resolution?](https://stackoverflow.com/questions/20689055/java-8-instant-now-with-nanosecond-resolution) – Christian Seifert Oct 07 '19 at 10:36
  • In class `Clock` shows, `Instant.now()` is inited by milliseconds. ```public Instant instant() { return Instant.ofEpochMilli(millis()); }``` – caisil Oct 07 '19 at 10:50
  • @caisil That has been improved since Java 9, but still doesn’t give nanosecond precision (on any platform that I know of). – Ole V.V. Oct 07 '19 at 10:52
  • @perdian this is not duplicate because I do not use getNano I use toNanos and it is different question – David Oct 07 '19 at 11:16
  • Couldn't you just store `Duration.between(startTime, endTime)` into a variable? – MC Emperor Oct 07 '19 at 11:26
  • do you really need nano-seconds? I suggest: [Avoiding Benchmarking Pitfalls on the JVM](https://www.oracle.com/technetwork/articles/java/architect-benchmarking-2266277.html), [Common benchmarking pitfalls](https://plumbr.io/blog/performance-blog/common-benchmarking-pitfalls), [Why Are Java Microbenchmarks Hard?](http://tutorials.jenkov.com/java-performance/jmh.html#why-are-java-microbenchmarks-hard) and others – user85421 Oct 07 '19 at 11:40
  • I still believe it is a duplicate of *Java 8 Instant.now() with nanosecond resolution?* The call to `getNano()` in that question was just to make it still clearer that the `Instant` has zeroes in the last 6 positions of the nanos, that is, effectively only millisecond precision. (I may consider closing as duplicate, but will at least await your counter-arguments before doing so.) – Ole V.V. Oct 07 '19 at 14:29
  • Does this answer your question? [How do I write a correct micro-benchmark in Java?](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – Johannes Kuhn Dec 30 '20 at 22:45

1 Answers1

0

I would go with @Pshemo's comment about Instant.now().

https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html#now-- :

This will query the system UTC clock to obtain the current instant.

Where the link leads to:

This may use System.currentTimeMillis(), or a higher resolution clock if one is available.

So it may or may not have a higher precision than milliseconds. In your case the "may not have" seems to apply.

tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • Is there a better approach to do what I am trying to do (how much time it takes to my programs to run)? – David Oct 07 '19 at 11:22
  • 1
    @UIUX: you can check the https://stackoverflow.com/questions/20689055/java-8-instant-now-with-nanosecond-resolution link from the comments. It provides a nano-resolution `Clock`, which can be used with `now()` (there is a `now(Clock)` variant, immediately below the one I linked). – tevemadar Oct 07 '19 at 11:27