Is using a flag or an if clause more efficient?

In a Java loop it is more efficient to use the boolean flag instead of the if ?

Take a look at these two bits of code.

Using the flag:

 public boolean isSomethingForAnyone() { boolean flag = false; for (Item item : listOfItems) { flag = flag || item.isSomething(); } return flag; } 

Using the if :

 public boolean isSomethingForAnyone() { for (Item item : listOfItems) { if (item.isSomething()) return true; } return false; } 

A method with an if is of course faster if isSomething() returns true on the first iteration. But is it faster on average or does branching slow it down so much that it is slower? Also, is the situation different if the cycle is faster? Here I used a for-each loop for simplicity, which in my opinion is slower than iterating over an array using a counter.

+7
java performance if-statement boolean
source share
3 answers

The two parts of the code are not completely equivalent.

Even if you only item.isSomething() as many times as you need (contrary to my original answer), the first version still continues to try to iterate over the remaining collection.

Imagine an implementation of item.isSomething() that changed the collection in which the item was found (if it returns true ). At this point, the first part of the code will throw a ConcurrentModificationException , suggesting that this is a "regular" collection, while the second part of the code will simply return true .

In fact, the second part of the code is more efficient: it only iterates over most of the list, as required to determine the answer, and not through all. It is possible that performance is not very important - especially if the collection is small - but it depends on the context.

What you consider more readable is another matter - most likely, the effectiveness will not be significant, although it depends on the context. Personally, I believe that the second version is more readable and also more effective, so I will always use it. (Well, I would add braces around the body of the if , but thatโ€™s it.)

+4
source share

Are you doing this cycle literally a billion times? if not, then the difference is probably impossible to measure.

you can look at the generated bytecode and see what exactly is happening, but the compiler and jit and vm itself will probably optimize any differences anyway.

0
source share

If you want to know what is โ€œfasterโ€ on your computer, run it.

If we want to know what instructions to follow, we can look at the bytecode.

For the first method, we get this ( javap -c call)

 Code: 0: iconst_0 1: istore_1 2: getstatic #2 // Field listOfItems:Ljava/util/List; 5: invokeinterface #3, 1 // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator; 10: astore_2 11: aload_2 12: invokeinterface #4, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z 17: ifeq 50 20: aload_2 21: invokeinterface #5, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object; 26: checkcast #6 // class Item 29: astore_3 30: iload_1 31: ifne 41 34: aload_3 35: invokevirtual #7 // Method Item.isSomething:()Z 38: ifeq 45 41: iconst_1 42: goto 46 45: iconst_0 46: istore_1 47: goto 11 50: iload_1 51: ireturn 

We are interested in the inner part of the loop, that is, lines 29-46 (lines 11-26 are iterator materials). So, about 10 instructions.

For the second method, we get the following:

  Code: 0: getstatic #2 // Field listOfItems:Ljava/util/List; 3: invokeinterface #3, 1 // InterfaceMethod java/util/List.iterator:()Ljava/util/Iterator; 8: astore_1 9: aload_1 10: invokeinterface #4, 1 // InterfaceMethod java/util/Iterator.hasNext:()Z 15: ifeq 40 18: aload_1 19: invokeinterface #5, 1 // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object; 24: checkcast #6 // class Item 27: astore_2 28: aload_2 29: invokevirtual #7 // Method Item.isSomething:()Z 32: ifeq 37 35: iconst_1 36: ireturn 37: goto 9 40: iconst_0 41: ireturn 

Interesting lines - 27-37. So, 7 instructions.

In terms of numbers, the second method comes from the top (note that we assume that all stack operations are performed the same way to execute).

0
source share

All Articles