1

In some situations we use a loop for convenience, but also could perform some operations "manually", example of handling MenuItems from my app:

  1. I create a little array of MenuItems and then iterate through it using an enhanced for loop.

    MenuItem[] fileActionsToLock ={ mMenu.findItem(R.id.action_share),
                                    mMenu.findItem(R.id.action_rename),
                                    mMenu.findItem(R.id.action_copy),
                                    mMenu.findItem(R.id.action_move) };
    for (MenuItem i : fileActionsToLock) {
        i.setEnabled(false);
        i.getIcon().setAlpha(100);
    }
    
  2. I apply the values separately to each MenuItem.

    mMenu.findItem(R.id.action_share).setEnabled(false);
    mMenu.findItem(R.id.action_share).getIcon().setAlpha(100);
    mMenu.findItem(R.id.action_rename).setEnabled(false);
    mMenu.findItem(R.id.action_rename).getIcon().setAlpha(100);
    mMenu.findItem(R.id.action_copy).setEnabled(false);
    mMenu.findItem(R.id.action_copy).getIcon().setAlpha(100);
    mMenu.findItem(R.id.action_move).setEnabled(false);
    mMenu.findItem(R.id.action_move).getIcon().setAlpha(100);
    

Since there are only a few elements, the performance is visually the same.

  • But which code works faster and why?
  • Also, at which point of time the fileActionsToLock array and the temporary MenuItem i will be garbage collected?

3 Answers 3

2

(1) I don't think either one will run noticeably faster than the other. The loop will have more overhead (e.g. see comments below about needing an iterator) but should take about the same amount of time to complete as if you just wrote out all the steps individually. Bottom line - you're not going to notice a difference. I'd use the loop because it probably makes your code easier to read.

(2) When something is garbage collected is hard to say. Generally, an object will be eligible for garbage collection when there are no more references pointing to it. How and when that actually occurs depends on the rest of your code and garbage collection your individual device's JVM is running. Different versions of android os have different kinds of garbage collectors. Some more about garbage collectors can be found here and also here

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

3 Comments

The manual one will be ever so slightly faster as it doesn't need to create an iterator unlike the enhanced for loop. It doesn't really make any difference though - especially with as few items as in this case. +1 on using a for loop, though. Significantly easier and scales much better.
Loop unrolling is a common optimization, and easier than combining statements into a loop. Writing it as a loop is not only clearer, it leaves the unrolling decision to the JVM, which has more data about the run time environment.
Doesn't a JVM with a JIT unwind loops on its own? The iterator is going to create more overhead and slow the overall process down initially as its created but I don't know that the actual code will execute faster out side of the loop.
1

The non-loop method is definitely faster. It's easier to compare a standard for loop because we know exactly what is happening, and so I will use that as an example. When the loop begins we are declaring variables checking that a condition is met, and then running the first block of code. After the first block of code is run we are then running a statement before checking the condition and then repeating.

All those extras (declaring an extra variable, checking the condition and running the final statement) are not happening in your second example. Less is happening and so it will always be quicker. That said, those extra operations take so little time or effort for modern computers that it really isn't worth worrying about. The difference is negligible. So small that they are unlikely to ever make any difference to your application. Even if a foreach loop were faster than a for loop, there are still operations being carried out that wouldn't be in your manual example.

When deciding when to use one or the other the criteria you should be using is readability, functionality, extensibility, etc. It's easier to read a loop, and it takes fewer lines of code. The loop can be used to dynamically check conditions so it is more functional. Finally a loop is more extensible. You can add more Objects to the looped array later without having to alter the code.

As for garbage collection, there shouldn't be any difference. They will both be eligible for collection the moment that no references to them exist in any accessible and running code.

Comments

0

You can read about it in this article. Manual way is generally faster then enhanced for loop, because it does not call any methods on iterator. It is also faster then normal for loop because it does not contain jump and increment instructions - but this is not really an issue because nobody will do manual operations on hundrets of elements to get few ms from CPU.

Comments

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.