Does it matter if you divide the loops into smaller loops?

If you have a for loop that has two main parts:

for (object o : objects){
doSomething(o);
doSomething2(o);
}

Does it really hurt performance if you split this cycle into 2 smaller cycles, for example?

for (object o : objects){
doSomething(o);
}

for (object o : objects){
doSomething2(o);
}
+4
source share
1 answer

Technically, yes, you have to repeat the iteration twice. By definition, with the exception of a narrow band of possible operations, this will be slower than repeating once. You should also select iterator objects twice, not once, etc. Etc. If you are dealing with a massive list, I suggest that two loops can cause more paging memory.

? , .


: (, ), , . , , , , , , -, . , , - ( ), , .

+6

Source: https://habr.com/ru/post/1615969/


All Articles