Breaking out of a nested loop

Started by
50 comments, last by ExErvus 7 years, 9 months ago

If you've got two for loops you're looking at O(n*n) execution times.

I'd be tempted to refactor the code and eliminate the for loops if possible but that's just me...


In all seriousness, I'd be interested to see that.

for(int i = 0; i < 100; ++i)
{
    if(((i / 10) - 1) == ((i % 10) + 1))
        break;
}
at least 1 is gone and we only need a normal break tongue.png

And suddenly the code is more complicated and about 10-100x slower, because the divisions are taking many more cycles than the other simple operations.


for(int i=0,n=0; n<10; n += ((i+1)==10), i+=((i+1)==10)*-9+((i+1)!=10)){
}
look ma, no division or additional branching tongue.png
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
Advertisement


for(int i=0,n=0; n<10; n += ((i+1)==10), i+=((i+1)==10)*-9+((i+1)!=10)){

}
look ma, no division or additional branching tongue.png


This is turning into a Coding Horrors post!

look ma, no division or additional branching tongue.png

int i = 2, j = 0;
look ma, no loops either ;)

This is turning into a Coding Horrors post!

Yes, yes it is.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

If you've got two for loops you're looking at O(n*n) execution times.I'd be tempted to refactor the code and eliminate the for loops if possible but that's just me...

In all seriousness, I'd be interested to see that.
for(int i = 0; i < 100; ++i)
{
    if(((i / 10) - 1) == ((i % 10) + 1))
        break;
}
at least 1 is gone and we only need a normal break :P
And suddenly the code is more complicated and about 10-100x slower, because the divisions are taking many more cycles than the other simple operations.
for(int i=0,n=0; n<10; n += ((i+1)==10), i+=((i+1)==10)*-9+((i+1)!=10)){
}
look ma, no division or additional branching :P

Anyone else who is going to stop using nested loops at all?

Anyone else who is going to stop using nested loops at all?


I'll use a goto statement and face the wrath of the dreaded velociraptor (see here) than write that mess!

Honestly, I think it's hard to do better than the lambda solution, as far as clarity goes


    int i, j;
    [&] () {
        for(i = 0; i < 10; i++) {
            for(j = 0; j < 10; j++) {
                if(i - 1 == j + 1)
                    return;
            }
        }
    }();

I thought you were joking at first, but that's a decent solution. Or at least a less-worse solution.

If you've got two for loops you're looking at O(n*n) execution times.I'd be tempted to refactor the code and eliminate the for loops if possible but that's just me...

In all seriousness, I'd be interested to see that.

for(int i = 0; i < 100; ++i)
{
    if(((i / 10) - 1) == ((i % 10) + 1))
        break;
}
at least 1 is gone and we only need a normal break tongue.png

And suddenly the code is more complicated and about 10-100x slower, because the divisions are taking many more cycles than the other simple operations.

for(int i=0,n=0; n<10; n += ((i+1)==10), i+=((i+1)==10)*-9+((i+1)!=10)){
}
look ma, no division or additional branching tongue.png


Anyone else who is going to stop using nested loops at all?


Help! How do I break out of nested quotes??

Python exceptions are not meant to represent exceptional error conditions alone, so it isn't an "abuse." As you point out, sequence iteration is terminated by raising StopIteration. This kind and level of exception usage is key to Python's dynamic, introspective nature.


How so? What is done with exceptions that don't represent an error condition that can't be done with any other construct and is so essential to Python's nature?


It is perhaps helpful to think of Python exceptions as "execution interruptions": they are an all-purpose, low-overhead synchronous notification of a change to the state you are presently interrogating. Python is highly dynamic, with deep introspection and meta-class hacking abilities front and center. You can treat objects as dictionaries, iterate over their members and examine the existence of attributes without any special syntax. A uniform means of indicating surface limits, then, is the use of exceptions—and that uniformity is prized in Python, due to the Zen of Python belief that "there should be one, and preferably only one, obvious way to do a thing."

Essentially, in Python, exceptions represent any interruption of your routine code, any special cases at all. Bounds limits, zero-division, end of file marker, etc. By virtue of being exceptions rather than explicit stateful return values, they can propagate up the call stack and be handled at the logically convenient place.

It's perhaps a little different, but it works really well in practice.

If you've got two for loops you're looking at O(n*n) execution times.I'd be tempted to refactor the code and eliminate the for loops if possible but that's just me...

In all seriousness, I'd be interested to see that.

for(int i = 0; i < 100; ++i)
{
    if(((i / 10) - 1) == ((i % 10) + 1))
        break;
}
at least 1 is gone and we only need a normal break tongue.png

And suddenly the code is more complicated and about 10-100x slower, because the divisions are taking many more cycles than the other simple operations.

for(int i=0,n=0; n<10; n += ((i+1)==10), i+=((i+1)==10)*-9+((i+1)!=10)){
}
look ma, no division or additional branching tongue.png


Anyone else who is going to stop using nested loops at all?


Help! How do I break out of nested quotes??

break;

If you want to see a real masterpiece, look at page 3 or 4 of the comments here https://derpibooru.org/883523

https://derpibooru.org/883523


Not a site i ever expected would be linked to on gamedev :p
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

This topic is closed to new replies.

Advertisement