Question

I have the following code in C++ here:

#include <iostream>

int main(int argc, const char * argv[])
{
    goto line2;
line1:
    std::cout << "line 1";
    goto line3;
line2:
    std::cout << "line 2";
    goto line1;
line3:
    std::cout << "line 3";
    goto line4;
line4:
    std::cout << "Hello, World!\n";
    return 0;
}

If I made a larger program of lets say 10,000 lines of code and I decide I am never going to use functions that I write myself, I only use goto statements. I only use global variables. I am slightly insane in terms of best practices, but its for a very specific purpose. The question is, would this be efficient to jump around with goto statements? What if I have 1000 goto labels?

Do the goto statements translate directly into machine code which tells the computer just to JUMP to a different memory address? Is this a lower cost in the machine to jump around like this when compared with the cost to call a function?

I wish to know as I want to write a very efficient program to do some computations and I need to be very efficient without resorting to Assembly/Machine code.


No need to tell me this is a bad idea in terms of maintenance, understandability of code, best practices, I'm very aware of that, I just wish to have an answer to the question. I don't want any debate between whether its good to use function calls or good to use goto.


To clarify the question, I am concerned in this case of using gotos only with a 10,000 line program as to how it would compare with a traditional program using functions. There are multiple ways to compare and contrast between these two programs, for example how would the CPU cache perform. What sort of saving would it give without function calls. Without a call stack, how would this impact on the CPU cache, as CPU caches usually keep the stack close. Would there be therefor a case where it is likely to have a negative performance hit due to the cache not being utilized correctly. What is the actual cost of calling a function as compared to a jump in terms of time efficiency. There's a lot of ways to compare and contrast the two styles of programming in terms of efficiency.

Was it helpful?

Solution

Do the goto statements translate directly into machine code which tells the computer just to JUMP to a different memory address?

Yes.

Is this a lower cost in the machine to jump around like this when compared with the cost to call a function?

Yes.

However, when the compiler sees a function call, it doesn't have to actually generate code to call a function. It can take the guts of the function and stick them right in where the call was, not even a jump. So it could be more efficient to call a function!

Additionally, the smaller your code, the more efficient it will be (generally speaking), since it is more likely to fit in the CPU cache. The compiler can see this, and can determine when a function is small and it's better to inline it, or when it's big and better to separate it and make it a real function, to generate the fastest code (if you have it set to generate the fastest code possible). You can't see this, so you guess and probably guess wrong.

And those are just some of the obvious ones. There are so many other optimisations a compiler can do. Let the compiler decide. it's smarter than you. It's smarter than me. The compiler knows all. Seriously, Cthulhu is probably a compiler.

You said not to, but I'm going to say it: I highly advise you to profile your code before deciding to do this, I can almost guarantee it's not worth your time. The compiler (most of which are near-AI level smart) can probably generate as fast or faster code with regular function calls, not to mention the maintenance aspect.

OTHER TIPS

Do the goto statements translate directly into machine code which tells the computer just to JUMP to a different memory address?

Pretty much.

Is this a lower cost in the machine to jump around like this when compared with the cost to call a function?

A function call is going to make a pretty similar jump, but before you can make the jump, you have to set up the new stack frame for the new function, push on parameters according to calling conventions, and at the end set up any return value and unwind. Yes, it's probably faster to not do this.

I am slightly insane

Yes.

Yes, the machine code generated from goto will be a straight JUMP. And this will probably be faster than a function call, because nothing has to be done to the stack (although a call without any variables to pass will be optimized in such a way that it might be just as fast).

And God help you when something doesn't work with this code. Or when someone else has to maintain it.

1) The question is, would this be efficient to jump around with goto statements? What if I have 1000 goto labels?

From your small example with 4 goto labels, where you jump back and forth, no it is not efficient in terms of performance. To avoid overhead in function call mechanism, this method is disabling many other optimization which the compiler will automatically do for you. I am not listing them, but this worth reading.

2) Do the goto statements translate directly into machine code which tells the computer just to JUMP to a different memory address?

YES (As others correctly pointed out)

3) Is this a lower cost in the machine to jump around like this when compared with the cost to call a function?

YES, only if your compiler is pre historic, and doesn't have any optimization mechanism in built. Otherwise NO.

And I am not talking about best practices..

hard to answer your query precisely, it depends on complexity of your program and use of goto statements within.

goto statement is equivalent to unconditional jump instruction (e.g. jmp ). The scope of goto will be within the file.

Ritchie suggest that avoid using goto statement, and if still want to/have to use goto statement then use it in top-down approach, Dont use it in bottom-up approach.

Well these are text book details.

Practically you should be very sure where to use goto statement and after goto jump where your program flow will be, otherwise as you mentioned with 1000 goto statements it will be difficult for you also to decide the program flow, forget about others. So further improvement in your program will be very very difficult.

There are plenty of other facilities such as looping, conditional statements, break and continue statements, functions etc to help you avoid such problems.

Hope it helps.....

In short, 'GoTo' statements can be efficient, but that is really how they are used. According to Herbert Schildt (C++ from the GROUND UP), "There are no programming situations that require the use of the goto statement -- it is not an item necessary for making the language complete." Ultimately, the primary reason for many programmers disliking the statement is that goto statements tend to clutter your code and/or make it very difficult to read because, as per the name, gotos can jump from place to place. With that being said, there are times where a goto statement can reduce clutter as well as make code more efficient, but that is totally dependent upon how you use them and the context that they are used in. Personally, I would recommend using function calls, as oppose to several goto statements; others may disagree.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top