Please save yourself from developing bad habits early in your programming careers. Never use goto. Also, conforming to modern coding conventions will allow you to be accepted amoung the OOP community. People won't laugh at you and your code. :)
Printable View
Please save yourself from developing bad habits early in your programming careers. Never use goto. Also, conforming to modern coding conventions will allow you to be accepted amoung the OOP community. People won't laugh at you and your code. :)
would using functions instead of goto slow down larger prorgams?
Generally, when you are attempting to make your program faster, you use a profiler or some other tool to identify what parts of your code need to be optimized. In most cases, you need only change your algorithm, and not the programming structures you are using to implement that algorithm.
It is possible, in some rare cases, that you might obtain a performance benefit from using goto over certain other options, however, you might as well use assembly if you really want the performance gain. If you are a C++ programmer, and you are worried about that type of overhead, then you should be using C. In most cases I would guess that the fear of the program being slower is unfounded and actually leads to more time being spent debugging and maintaining the goto than is saved by using it.
Wouldn't void functions that take no parameters be a close if not exact equivalent of a goto? It's a jump in code execution, with no added stack allocation/deallocation for parameters that I can think of.
no. functions translate to CALL and RETURN type of assembly instructions. No parameter makes it so that less stuff needs to be PUSHed and POPed, but still is much slower. Aside from that, goto is one direction and doesn't return.Quote:
Originally Posted by Hunter2
yes, I think functions were a bad example. however lets use a "for instance". In the code example you presented you changed a while loop to a conditional goto. This gained nothing in the area of speed. The dissasembly should show pretty much the same output.Quote:
Originally Posted by cgod
For the most part, code that uses a goto can directly translate to an easier to read construct. There are some occasions (when you haven't put any thought into your algorithm) that there is no direct translation and it would take a little more effort to convert the function. Best idea is to just not use it as a rule. It gains you nothing and causes a world of pain.
I stand corrected.Quote:
no. functions translate to CALL and RETURN type of assembly instructions. No parameter makes it so that less stuff needs to be PUSHed and POPed, but still is much slower.
I was thinking of a block of code that you goto, then at the end of the block there's another goto that jumps back to where you were. On second thought though, I realize that that still wouldn't work.Quote:
Aside from that, goto is one direction and doesn't return.