2

With LibGDX comes a example which use the MVC pattern.

the updater looks something like this

List<Enemies> enemies = new List<Enemies>();

public void update() {
updateEnemies();
checkCollision();
}

void updateEnemies() {
 for each enemy //Loop enemies list{
   enemy.update();
  }

}

void checkCollison(){
   for each enemy//Loop enemies list{
    if(enemy.overlaps(hero.bounds) {
       //Do stuff
    }
   }
}

Now it loops through enemeies list twice. Should I do this? Maybe not one small list but I want to add lots of lots of lists so should I merge them?

Cheers!

AndroidXTr3meN
  • 399
  • 2
  • 9
  • 19

6 Answers6

5

The iteration itself is cheap, and is unlikely to be the source of any performance problems. Nothing, however, is guaranteed; if you want to understand the performance characteristics of your program, use a profiler.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

Be warned. In updating all enemies they all take new positions. And then collisions are checked. If you would do in a loop: update one enemy and check collision, you would check on obsolete positions of some enemies. This might be irrelevant, or not. A java comment would be in order: // First update all enemies: ... // Now we can for the new positions: .

As answer: overhead is minimal.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • overhead?...sorry what does that mean? – AndroidXTr3meN May 22 '12 at 15:59
  • I was going to write something similar, until I realized that the code only checks for collisions with the *hero*, whose position doesn't appear to be changed by either of the loops. – NPE May 22 '12 at 16:14
  • AndroidXTr3meN: overhead = costs; really tiny as others said. For @aix thanks for stating that. – Joop Eggen May 22 '12 at 16:33
2

From the code above, one can see that the performance should be good as the loop you are using is performance-friendly:

for (Enemies enemy : enemies)
{
    //Do something here
}

According to this.

Community
  • 1
  • 1
GingerHead
  • 8,130
  • 15
  • 59
  • 93
0

Doing:

for each element in collection
   operationA();
   operationB();
end for

takes basically the same time as doing:

for each element in collection
   operationA();
end for
for each element in collection
   operationB();
end for

Yet, the second option may be clearer as each loop has a specific goal and not everything is mixed up in the same loop.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • 1
    I find that hard to believe it takes the same time. If my collection is 12 trillion items it will be twice as long. – Woot4Moo May 22 '12 at 15:26
  • I agree, it would take more time. But depending on the size of the list it could be negligible. – CaffeinatedCM May 22 '12 at 15:29
  • Actually, n * (a + b) = n * a + n * b. There might be a little overhead if the execution time of one operation is VERY short! – Jean Logeart May 22 '12 at 15:36
  • Well im thinking that if I merge them and enlarge my game it might ruin the design and make it unclear. or? – AndroidXTr3meN May 22 '12 at 15:39
  • It might. All I am saying is that it is often better to have n blocks of code doing n specific things you clearly understand and can easily maintain than having one block of code doing n things (and sometimes mixing them!). And that's specially true if there is no performance issue. – Jean Logeart May 22 '12 at 15:44
  • Thanks! I design the code so I can easily change if I change my mind or I get performance Issue – AndroidXTr3meN May 22 '12 at 15:47
  • In general, you should write your code to be readable, measure performance, and then fix bottlenecks if you need better performance. You can conjecture all day about what will make it run faster, but you use measurements to guarantee you get the "bang for your buck" when you add optimizations – jeff May 22 '12 at 15:50
0

In theory, once the code is executed, the compiler might merge both loops together, or optimize it in a better way. Sometimes things do not get exactly executed the way they seem to be put together.

Basilio German
  • 1,801
  • 1
  • 13
  • 22
0

Merging loops will introduce new overhead, unexpected intervals... Instead, using same loops for different methods doesn't affect performance in practice, it keeps your code clear, more readable and maintainable. Catching exceptions and other custom tasks with each method can be easily handled if those methods.

Vishal
  • 2,161
  • 14
  • 25