3

Can somebody direct me to right path for

Why in the following code, there is a gap of a second between start and end msg?

System.out.println("Start:" + LocalTime.now());
for (int i = 0; i > -1; i++) { /*Infinite loop*/ }
System.out.println("End  :" + LocalTime.now());

I tried to find out if DCE takes time for this type of code but couldn't find much.

Eagle_Eye
  • 1,044
  • 1
  • 14
  • 26
  • 9
    That loop is not infinite. It will loop until `i` overflows and eventually becomes -1. – marstran Aug 18 '18 at 14:33
  • 6
    What are you asking? It takes a second to overflow `int` - those 2.4bn iterations take time. Put another way, your loop isn't infinite - it's just long. – Boris the Spider Aug 18 '18 at 14:35
  • 1
    Where does dead code elimination come into it? – Boris the Spider Aug 18 '18 at 14:38
  • @BoristheSpider Probably the OP thought that the `for` loop would be removed entirely since it is empty – Thiyagu Aug 18 '18 at 14:39
  • @marstran Did you mean *eventually overflows to become negative*? – Thiyagu Aug 18 '18 at 14:41
  • 3
    Also note that your measurement is kind of *broken* as it measures how long it takes to print the first text. Printing takes long, possibly longer than the rest of the code you measure. By that, it may dominate the measurement and the resulting time gives you the wrong impression. Thus, include prints in your measurements. Also, `LocalTime.now()` is bad for measuring durations, as it **drifts**. Use `System.nanoTime()` and take the difference. – Zabuzard Aug 18 '18 at 14:48
  • @Zabuza, you beat me to it. The results can't be exactly accurate because the amount of time printing takes (which is relatively longer) is not accounted for. – Taslim Oseni Aug 18 '18 at 14:52
  • 2
    @marstran it does not become -1 but breaks the loop at -2147483648... well before reaching -1... I think you meant less than -1 – devm Aug 18 '18 at 14:52
  • The question should be changed to "why this loop is not infinite" or similar. I don't see how DCE is related here. – user202729 Aug 18 '18 at 14:53
  • 1
    (in that case there is some duplicates [here](https://stackoverflow.com/questions/42107103/java-why-does-the-following-infinite-loop-terminate-without-any-error-except), [here](https://stackoverflow.com/questions/21085524/why-incrementing-int-primitive-in-while-loop-does-not-loop-forever) or [here (closest)](https://stackoverflow.com/questions/10968733/why-does-incrementing-a-java-int-eventually-result-in-a-negative-number)) – user202729 Aug 18 '18 at 14:55
  • @Mahi That what I meant ;) – marstran Aug 18 '18 at 15:12

1 Answers1

6

As you have defined "i" as int which ranges from -2,147,483,648 to 2,147,483, 647. As soon as it reaches -2147483648 (your code loops starting from 0 and increment 1) , the condition becomes false and the loop breaks.

    int i;
    System.out.println("Start: " + LocalTime.now());
    for (i = 0; i > -1; i++) { /*Infinite loop*/ }
    System.out.println("End  :" + LocalTime.now());
    System.out.println("i  :" + i);

Try above and you will see the value at the end is -2147483648 and hence it comes out of the loop.

devm
  • 227
  • 1
  • 12