2

I have 2 pieces of code:

First one:

List<Integer> integers = new ArrayList<>();
integers.add(0);
integers.add(1);
//Assume that list will always have the same values with loop indices.
//Also any of the methods will not change the size and elements of the list.

for (int i=0;i<integers.size();i++) {

    if (0 == integers.get(i)) {
        foo();
    }
    else if (1 == integers.get(i)) {
        bar();
    }
}

Second one:

foo();
bar();

I know both code snippets are doing the same thing but is there any difference in performance or does JVM doing something to optimize the first snippet in compile time or runtime?

5
  • 3
    Have you tried? But no, I wouldn't expect the JIT to optimize much in this case. Commented Nov 29, 2018 at 11:30
  • Do you have any idea what if we wouldn't use an Arraylist and use constants(that is for(int i=0;i<2;i++) and if conditions will be checked with i==0 and i==1 directly) Commented Nov 29, 2018 at 11:58
  • That would be a different story - in that case, it might choose to unroll the loop and might be also able to figure out which branch gets called, rendering them equivalent. Whether or not a specific compiler/JIT actually does this for the given code would have to be tested. Commented Nov 29, 2018 at 12:03
  • Can you give me some reference about this, because my question was actually related to that topic.Thanks in advance. Commented Nov 29, 2018 at 12:07
  • The problem are the method calls - the JVM does not see through them and do do nothing at all. However, when the called methods are sufficiently small, then they may get inlined and heavy optimizations may start which may lead to your second snippet. I'm afraid, it does not happen in this case as it's probably too complicated. Commented Dec 2, 2018 at 4:05

3 Answers 3

2

The compiler/JIT won't be able to do much for the first snippet.

It cannot prove that successive calls to integers.get(i) will always yield the same result, so it is not allowed to reuse the result of the first call, and it cannot even tell that one of foo() or bar() will always be executed.

The code appears simple enough for humans, but the compiler would have to make assumptions about the implementation of ArrayList that will be loaded at runtime, and it will not do that.

Sign up to request clarification or add additional context in comments.

Comments

0

The second one is more efficient since you are doing a lot more operations in the first snippet, which (obviously) takes more time to execute and evaluate.

Generally you can conclude that more for loops/operations/if statements means a lesser performance.

Java will just evaluate and does not simplify it to become like the second one.

3 Comments

Can JVM predict that list has a strict size as the same with indices and make some optimization?
@AlperenÜretmen No it won't, it would have to check the size() to make sure that the list does not change in size, since the size is variable. it will not do that
@AlperenÜretmen it would have to make sure that the runtime type of integers is really ArrayList, then look up the implementation of size(), somehow figure out that this depends only on one field and proof that this field can not be modfied somehere whithin the loop body - that would be a lot of complicated work and is not even possible in the general case.
0

Things that take time in the first snippet: Creating list object , adding elements to a list, putting a value to a variable, checking if a variable is smaller than another one, incrementing a variable, jumping in code (breaking out of loop, and if statements), calling methods and executing them.

in second sinnpet : calling methods and executing methods.

If you put both these snippets in for loops, and execute them 1000 or more times, you'll see a big difference.

1 Comment

Thanks, but my problem is that whether JVM can figure out second snippet does the same job with the first one(since the list is created once and not modified after that) and does some optimization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.