2

I've noticed that both of the following loops take the same amount of time: ~ 1.2 seconds.

double count = Math.pow(10, 9);
for (int i = 0; i < count; i++) {
    int y = 10;
    int x = 3*y;
    x = 100*y;
    int[] arr = new int[3];
    arr[0] = 1;
}
double count = Math.pow(10, 9);
for (int i = 0; i < count; i++) {
    //nothing
}

Why would they finish in the same time? These for-loops only increment and do 1 comparison, so 2 actions per iteration. I would think the first one would take about 3 times longer to finish, since it does 7 actions whereas the second one does 2.

akuzminykh
  • 4,522
  • 4
  • 15
  • 36
Will Kanga
  • 652
  • 1
  • 6
  • 12

3 Answers3

2

The answer is not because of optimization (or solely thereof - the byte code is generated). It is because the loop is a floating point loop and floating point operations are notoriously expensive. So the internal code is relatively faster or insignificant to the overall running time (as was alluded to by @j11john).

If you change the loop so it is int based, the two forms will run at significantly different speeds.

        long start = System.nanoTime();
        int count = (int)Math.pow(10, 9);
        int x = 0;
        for(int i = 0;i<count;i++) {
            int y = 10;
             x = 3*y;
            x = 100*y*x;
            int[] arr = new int[3];
            arr[0] = 1;
        
        }
        System.out.println((System.nanoTime()-start)/1_000_000_000.);
    }

8.4248E-5 vs 0.047076639 The latter time with the extra code.

So the similarity in both running times (as observed by the OP) is related to the nature of the loop (int vs double) and has very little to do with the code within the loop.

WJS
  • 36,363
  • 4
  • 24
  • 39
  • It boggles my mind that JIT is not optimizing that. – akuzminykh Sep 22 '20 at 22:18
  • You mean not converting to an int? I would agree. – WJS Sep 22 '20 at 22:18
  • Yes, even if `double` can store much more values than `int`, I don't see why there shouldn't be just a simple check or whatnot to optimize the loop. – akuzminykh Sep 22 '20 at 22:20
  • And when I examined the byte code I was surprised it was there. All local values which are never referenced outside the loop. – WJS Sep 22 '20 at 22:21
  • 1
    I like the observation you've made but I'd also explicitly mention that the loop-body is insignificant compared to the difference `double` vs `int`; because that's what OP asked about. – akuzminykh Sep 22 '20 at 22:37
0

Surely the inside code of the loop isn't cpu expensive so is probably going to have the same time.

Dhiraj
  • 550
  • 6
  • 14
j11john
  • 29
  • 1
  • 7
  • 1
    Well, the code inside of the loop isn't it cpu expensive because the cpu capabilities increased a lot. So the two methods will take the same time :) If you want, add a Thread.sleep() simulating an expensive execution inside the first method. – j11john Sep 22 '20 at 21:54
  • 1
    You are still speculating. – WJS Sep 22 '20 at 21:56
  • you mean that I should run an example? – j11john Sep 22 '20 at 22:00
  • 2
    Okay, I changed my mind. You are probably correct. But you didn't give a detailed explanation of why. the internal code is faster compared to a simple loop. – WJS Sep 22 '20 at 22:08
  • 1
    ok, thanks for your support – j11john Sep 22 '20 at 22:14
  • You still don't *explain* anything in your answer as WJS said in his deleted comment. Besides that, one sentence answers are far below the quality standard SO has IMO. Don't just state something: explain it as if thousands of other people will find this answer and can *learn* something from it. E.g. check out [this](https://stackoverflow.com/a/62502133/12323248) answer to a similar post. – akuzminykh Sep 22 '20 at 22:30
0

If code inside the loop doesn't affect code after it compiler will skip all operations inside the loop to optimize performance.

Check the following link for more details: https://en.wikipedia.org/wiki/Java_performance

WJS
  • 36,363
  • 4
  • 24
  • 39