简体   繁体   中英

Enhanced for-loop vs handling objects manually

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

  1. I create a little array of MenuItem s 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?

(1) I don't think either one will run noticeably faster than the other. The loop will have more overhead (eg 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

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 Object s 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM