in reply to faster way for multiple foreach loops

As suggested (but not explained) by ikegami in the first reply: whenever you need to do a set of nested loops, the general rule is that it's slightly more efficient (other things being equal) to arrange for the outer-most loop to have the fewest iterations, and the inner-most loop to have the most.

Apart from that, you want no more than the absolute minimum set of steps necessary to happen within the inner-most loop -- move as many things to the outer-most loop (or as far outward from the inner-most loop) as possible.

And that's about all you can do, without reducing the number of layers or iterations.

  • Comment on Re: faster way for multiple foreach loops