Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

3
  • 1
    You can very often just flatten a nested loop and it still takes the same time. take for 0 to width;for 0 to height; You can instead just put for 0 to width times height. Commented Jan 30, 2019 at 15:05
  • @jgmjgm - yes, at the risk of complicating the code inside the loop. Flattening can simplify that too sometimes, but more often you're at least adding the complexity of recovering the indices that you actually want. One trick for that is using an index type that factors all the loop nesting out into the logic for incrementing a special compound index - you're not likely to do that just for one loop, but maybe you have multiple loops with similar structures or maybe you can write a more flexible generic version. Overheads for using that type (if any) can be worth it for clarity. Commented Jan 30, 2019 at 23:55
  • I'm not suggesting it as a good thing to do but as just how surprisingly simple it can be to turn two loops into one but still not have an impact on the time complexity. Commented Feb 10, 2019 at 7:37