Jump to content

3 nested for loop

ichihgo

in general is there a way I can do the same job of 3 nested for loop but faster

Link to comment
Share on other sites

Link to post
Share on other sites

Easier to understand though. 3 nested loops just sounds like a mindfuck lol

 

3 nested loops are not so bad tbh.

Link to comment
Share on other sites

Link to post
Share on other sites

You might be able to unroll a loop or just figure out a better way to code it. Those are really your only options

 

Recursion?

When you call, you push ESP and EBP to the stack, along with any parameters... and if you have 3 levels of recursion, that's gonna be a nightmare.

Link to comment
Share on other sites

Link to post
Share on other sites

Even if the loops are necessary there are efficient and inefficient ways of implementing them. Do it wrong and you'll get cache thrashing, which will reduce performance by orders of magnitude.

Link to comment
Share on other sites

Link to post
Share on other sites

Even if the loops are necessary there are efficient and inefficient ways of implementing them. Do it wrong and you'll get cache thrashing, which will reduce performance by orders of magnitude.

Any recommendations on not having loops do cache thrashing, other than the obvious like using fewer/smaller variables?

Link to comment
Share on other sites

Link to post
Share on other sites

Any recommendations on not having loops do cache thrashing, other than the obvious like using fewer/smaller variables?

 

Well, a basic one is making sure to read multidimensional loops in the right order. When you access memory, a line of memory will be read and stored in the CPU cache. This works great if you are reading across rows in the array. But if you are reading down columns, the CPU will be forced to go to memory for each operation instead of being able to rely on the much faster cache until getting to the next row.

 

Google for loop tiling and strip mining. Compilers will try to do optimizations like this on their own, but the more you help them the better the performance potential.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×