• Create Account

### #ActualHodgman

Posted 30 November 2012 - 07:31 AM

how would you replace the goto in this bit with an exception

I'm of the opinion that exceptions as implemented in C++ are too costly to be used in the same fashion as they are in C#/Java. I use exceptions in C#, but never ever use them in C++. </opinion>

Anyway, as above you can split that work out into a function, or just put your condition into the for loops:
void function()
{
found = false;
for (int bar =0; !found && bar < 100; ++bar)
for(int foo = 0; !found && foo < 100; ++foo)
if (foo * bar == 100)
found = true;
}
Pretty much the only time I use goto is when you've got a complex function that's allocating multiple resources and has multiple points of failure, where they all goto a cleanup section at the bottom. However, RAII solves this in C++, so it's not necessary any more, most of the time.

Out of interests sake, because I assumed they'd all generate pretty much the same Asm, I tested my C++ compiler (MSVC9 /O2) with the above loop+flag, the previously posted function+return, and the original goto-as-double-break variants. In all cases the loop+flag was the slowest, so I'll normalize the results against it and call it's speed 100%.
If the break condition is hit quickly (e.g. foo*bar == 100), then the function+return solution finished in 94% of the time, and the goto in 92% of the time. If the break condition isn't hit (e.g. foo*bar == 99999), then function+return finished in 67% of the time, and goto in 99% of the time...
[edit2]Added a 4th test for throwing a bool to escape the two loops. If the condition isn't met, it weighed in at 103%, but if the condition is met quickly as before, it took 206358% as long...
So, uh, yeah, I'd go with Bacterius' code.

### #5Hodgman

Posted 30 November 2012 - 07:28 AM

how would you replace the goto in this bit with an exception

I'm of the opinion that exceptions as implemented in C++ are too costly to be used in the same fashion as they are in C#/Java. I use exceptions in C#, but never ever use them in C++. </opinion>

Anyway, as above you can split that work out into a function, or just put your condition into the for loops:
void function()
{
found = false;
for (int bar =0; !found && bar < 100; ++bar)
for(int foo = 0; !found && foo < 100; ++foo)
if (foo * bar == 100)
found = true;
}
Pretty much the only time I use goto is when you've got a complex function that's allocating multiple resources and has multiple points of failure, where they all goto a cleanup section at the bottom. However, RAII solves this in C++, so it's not necessary any more, most of the time.

Out of interests sake, because I assumed they'd all generate pretty much the same Asm, I tested my C++ compiler (MSVC9 /O2) with the above loop+flag, the previously posted function+return, and the original goto-as-double-break variants (taking care to add enough side-effects so the compiler didn't optimize out the functions/loops completely, and warming up the cache before testing). In all cases the loop+flag was the slowest, so I'll normalize the results against it and call it's speed 100%.
If the break condition is hit quickly (e.g. foo*bar == 100), then the function+return solution finished in 94% of the time, and the goto in 92% of the time. If the break condition isn't hit (e.g. foo*bar == 99999), then function+return finished in 67% of the time, and goto in 99% of the time...
[edit2]Added a 4th test for throwing a bool to escape the two loops. If the condition isn't met, it weighed in at 103%, but if the condition is met quickly as before, it took 206358% as long...
So, uh, yeah, I'd go with Bacterius' code.

### #4Hodgman

Posted 30 November 2012 - 07:27 AM

how would you replace the goto in this bit with an exception

I'm of the opinion that exceptions as implemented in C++ are too costly to be used in the same fashion as they are in C#/Java. I use exceptions in C#, but never ever use them in C++. </opinion>

Anyway, as above you can split that work out into a function, or just put your condition into the for loops:
void function()
{
found = false;
for (int bar =0; !found && bar < 100; ++bar)
for(int foo = 0; !found && foo < 100; ++foo)
if (foo * bar == 100)
found = true;
}
Pretty much the only time I use goto is when you've got a complex function that's allocating multiple resources and has multiple points of failure, where they all goto a cleanup section at the bottom. However, RAII solves this in C++, so it's not necessary any more, most of the time.

Out of interests sake, because I assumed they'd all generate pretty much the same Asm, I tested my C++ compiler (MSVC9 /O2) with the above loop+flag, the previously posted function+return, and the original goto-as-double-break variants (taking care to add enough side-effects so the compiler didn't optimize out the functions/loops completely, and warming up the cache before testing). In all cases the loop+flag was the slowest, so I'll normalize the results against it and call it's speed 100%.
If the break condition is hit quickly (e.g. foo*bar == 100), then the function+return solution finished in 94% of the time, and the goto in 92% of the time. If the break condition isn't hit (e.g. foo*bar == 99999), then function+return finished in 67% of the time, and goto in 99% of the time...
[edit2]Added a 4th test for throwing a bool to escape the two loops. If the condition isn't met, it weighed in at 103%, but if the condition is met quickly as before, it took 206358% as long...

### #3Hodgman

Posted 30 November 2012 - 07:19 AM

how would you replace the goto in this bit with an exception

I'm of the opinion that exceptions as implemented in C++ are too costly to be used in the same fashion as they are in C#/Java. I use exceptions in C#, but never ever use them in C++. </opinion>

Anyway, as above you can split that work out into a function, or just put your condition into the for loops:
void function()
{
found = false;
for (int bar =0; !found && bar < 100; ++bar)
for(int foo = 0; !found && foo < 100; ++foo)
if (foo * bar == 100)
found = true;
}
Pretty much the only time I use goto is when you've got a complex function that's allocating multiple resources and has multiple points of failure, where they all goto a cleanup section at the bottom. However, RAII solves this in C++, so it's not necessary any more, most of the time.

Out of interests sake, because I assumed they'd all generate pretty much the same Asm, I tested my C++ compiler (MSVC9 /O2) with the above loop+flag, the previously posted function+return, and the original goto-as-double-break variants (taking care to add enough side-effects so the compiler didn't optimize out the functions/loops completely, and warming up the cache before testing). In all cases the loop+flag was the slowest, so I'll normalize the results against it and call it's speed 100%.
If the break condition is hit quickly (e.g. foo*bar == 100), then the function+return solution finished in 94% of the time, and the goto in 92% of the time. If the break condition isn't hit (e.g. foo*bar == 99999), then function+return finished in 67% of the time, and goto in 99% of the time...

### #2Hodgman

Posted 30 November 2012 - 07:17 AM

how would you replace the goto in this bit with an exception

I'm of the opinion that exceptions as implemented in C++ are too costly to be used in the same fashion as they are in C#/Java. I use exceptions in C#, but never ever use them in C++. </opinion>

Anyway, as above you can split that work out into a function, or just put your condition into the for loops:
void function()
{
found = false;
for (int bar =0; !found && bar < 100; ++bar)
for(int foo = 0; !found && foo < 100; ++foo)
if (foo * bar == 100)
found = true;
}
Pretty much the only time I use goto is when you've got a complex function that's allocating multiple resources and has multiple points of failure, where they all goto a cleanup section at the bottom. However, RAII solves this in C++, so it's not necessary any more, most of the time.

Out of interests sake, because I assumed they'd all generate pretty much the same Asm, I tested my C++ compiler (MSVC9 /O2) with the above loop+flag, the previously posted function+return, and the original goto-as-double-break variants. In all cases the loop+flag was the slowest, so I'll normalize the results against it and call it's speed 100%.
If the break condition is hit quickly (e.g. foo*bar == 100), then the function+return solution finished in 94% of the time, and the goto in 92% of the time. If the break condition isn't hit (e.g. foo*bar == 99999), then function+return finished in 67% of the time, and goto in 99% of the time...

### #1Hodgman

Posted 30 November 2012 - 06:46 AM

how would you replace the goto in this bit with an exception

I'm of the opinion that exceptions as implemented in C++ are too costly to be used in the same fashion as they are in C#/Java. I use exceptions in C#, but never ever use them in C++. </opinion>

Anyway, as above you can split that work out into a function, or just put your condition into the for loops:
void function()
{
found = false;
for (int bar =0; !found && bar < 100; ++bar)
for(int foo = 0; !found && foo < 100; ++foo)
if (foo * bar == 100)
found = true;
}
Pretty much the only time I use goto is when you've got a complex function that's allocating multiple resources and has multiple points of failure, where they all goto a cleanup section at the bottom. However, RAII solves this in C++, so it's not necessary any more, most of the time.

PARTNERS