[Solved] Nested loops performance [closed]


Lets assume that the inner loop actually does something. FredrikRedin and niculare are, of course, correct, that the number of inner loop executions will be the same. Given that this is the case, the second example is preferred (slightly) because the second example has to set up the inner loop ten times, while the first sets up the inner loop 100 times. Total number of loops set up:

  • First example: 101
  • Second example: 11

Since setting up loops takes time, the second example is to be preferred, all other things being equal (which they never are). In general, if you can divide iterations between inner and outer loops, give more iterations to the inner loop, thereby decreasing the number of iterations of the outer loop.

4

solved Nested loops performance [closed]