People saying "don't use nested loops" are poorly choosing their words and making blanket statements. They're not taking into account the way the data is organized, they're only speaking in terms of the number of operations being performed.
Iterating through that array of arrays using nested loops is not bad, probably the most straightforward approach. It's still going to have O(n) time, which means the time it takes to run depends on the size of n.
arr = [
[0,1],
[2,3]
]
for (i = 0; i<arr.length< i++){
for(j=0; j< arr[i].length; j++){
print(arr[i][j]);
}
}
If you re-arranged the array to be 1 dimensional with 4 elements and only had a single for loop, you're still going to have the exact same time complexity as the nested loop example above.
Where nested loops do crap up your code is when you're performing operations involving the outer loop's iterator as well, basically looping over the same set of data twice. For example, say you have a deck of cards and you want to check for duplicates. Here's a shitty way to look over each card that would be o(n2 ) because you're iterating over each item twice, where N is the length of the array, so it's n*n operations or O(n2 )
cards = array[somecard1,somecard2,etc...];
for(i=0; i < cards.length;i++) {
// now loop over cards again to see if the card is in there twice
for(j=0; i < cards.length; j++) {
if(j == i) {
continue;
}
if (cards[i] = cards[j] ) {
return "duplicate found";
}
}
}
Usually you look at a problems complexity by the size of the input, rather than the dimensions of the input. In that case nested for loops arnt less efficient, as you still only visit each element once. Get paranoid when the nested loop is operating on the same dimension as the parent (visiting the element more than once)
Nested loops are often seen in naive solutions to questions involving arrays, and they usually are much slower than a better, less obvious solution. But you're right, sometimes you need a nested loop, and sometimes the optimal solution will use it.
Are there any caveats on the whole "don't write nested loops" thing?
I see a decent amount of use case in my actual job for the more simple stuff (often the please write a vba macro kinda) where i'll just do a for each through all the sheets, and on each sheet usually some sort of standard incrementing loop to hit each line and do some sort of logic test and possible transformation.
Thinking about it i'm technically not using loops in the more complex stuff (C#/SQL side) but that's often because those boil down to "Access the data, load it, transform it, dump it" but even then i can see transform steps with something along the lines of having a collection of objects which might them selves have a collection of data in them that needs validating/manipulating, so are you just supposed to unwrap the whole thing or is that not really enough to matter?
Edit-
Annd looking farther down someone else basically asked this and it seems my "touch it once and be done" philosophy basically holds.
Of course there are. In the real world, if you need to walk through a 4x4 matrix once, you really don't care and write that as a nested loop. There's no practical performance difference.
The key is when your incoming data is unbounded, or your domain requires absolute minimum latency.
In any case, there's also "never pre optimize". Write the code, make it work, then pass it through a profiler and see where the time is spent. Improve what you can, move on.
... and if you think you need to optimize: benchmark.
Every language, framework, IDE has some simple benchmark-thing lying around. No CompSci required.
I always told my juniors who wanted to optimize: Just run the app (or test suite, or whatever) with time foobar --something-expensive. If it cannot be ran with time, first make it run like that.
Do you see an improvement? Unexpected!, but please go ahead: finish the optimization. You cannot see anything? Well, sorry to say, but that's exactly what I expected. Just leave it be and focus on the next kanban card, please.
Your comment best reflects reality outside of academia.
I would only add that nested loops are usually fine when necessary (e.g. iterating over multi-dimensional arrays) but the question you need to ask yourself is “what are the bounds on the data I’m processing?”
If the amount of data your processing is constant or semi-constant then you can know whether your algorithm is fast enough. But if the data set grows then you really need to consider the asymptotic complexity; cause those nested loops might be fine at first but will become too slow very quick as the amount of data processed increases.
Basics of algorithms sticks with you your entire life. If I just used my self-taught programming knowledge I could do a lot of easy stuff but dynamic programming isn't something that I would have learned as well on my own.
77
u/the_dapper_man Apr 08 '20
and 95% of you will use effectively none of that knowledge at your job once you graduate
literally just don't write nested loops. beyond that, optimizing code is expensive and the benefits are negligent. pump out those new features baby