Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are goto statements efficient when compared to calling functions? [closed]

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.

like image 254
Phil Avatar asked Jan 07 '13 05:01

Phil


People also ask

What is the biggest problem with goto statement?

"The GOTO statement is generally considered to be a poor programming practice that leads to unwieldy programs. Its use should be avoided."

What is the advantage of goto statement?

Goto is a jump statement that can alter the normal flow of execution of code. Using the goto statement, you can not only jump to a part of the code below the current flow but also a part above the current flow. This also enables goto statements to initiate loops in the program, without using for or while in the code.

Why goto statement should be avoided?

NOTE − Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten to avoid them.

What are the advantages and disadvantages of goto statement?

Advantage: using goto statement you can alter the normal sequence of the program execution so it gives the power to jump to any part of program. Disadvantages: It is always recommended not to use goto statement as this reduces the readability of the program.


4 Answers

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.

like image 183
Seth Carnegie Avatar answered Oct 16 '22 07:10

Seth Carnegie


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.

like image 38
James Avatar answered Oct 16 '22 09:10

James


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..

like image 4
Krishnabhadra Avatar answered Oct 16 '22 09:10

Krishnabhadra


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.

like image 3
DWright Avatar answered Oct 16 '22 07:10

DWright