与调用函数相比,goto语句有效吗? [英] Are goto statements efficient when compared to calling functions?

查看:159
本文介绍了与调用函数相比,goto语句有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中具有以下代码:

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;
}

如果我编写了一个较大的程序,说10,000行代码,而我决定不再使用自己编写的函数,那么我只会使用goto语句.我只使用全局变量.就最佳做法而言,我有点发疯,但这是出于非常特定的目的.问题是,使用goto语句跳转会很有效吗?如果我有1000个goto标签怎么办?

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?

goto语句是否直接转换为机器代码,该代码告诉计算机将JUMP跳转到另一个内存地址?与调用函数的成本相比,这样在机器中跳动的成本是否较低?

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.

在维护,代码的可理解性,最佳实践方面,无需告诉我这是一个坏主意,我对此非常了解,我只想对这个问题有一个答案.我不想在使用函数调用还是使用goto之间进行争论.

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.

为澄清这个问题,在这种情况下,我对仅在10,000行程序中使用gotos的情况感到担忧,与使用功能的传统程序相比,它如何?有两种方法可以在这两个程序之间进行比较和对比,例如,CPU缓存的性能如何.如果没有函数调用,它将节省多少费用.如果没有调用堆栈,这将如何影响CPU缓存,因为CPU缓存通常会使堆栈保持关闭状态.因此是否存在由于缓存使用不正确而导致性能下降的问题.与时间效率方面的跳跃相比,调用函数的实际成本是多少?有很多方法可以在效率方面比较和对比这两种编程风格.

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.

推荐答案

goto语句是否直接转换为机器代码,该代码告诉计算机将JUMP跳转到另一个内存地址?

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?

是的

但是,当编译器看到函数调用时,它不必实际生成代码即可调用函数.它可以利用函数的胆量,并将其固定在调用的位置,甚至不跳转.因此,调用函数可能更有效!

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!

此外,您的代码越小(通常来说),它的效率就越高,因为它更可能适合CPU缓存.编译器可以看到这一点,并可以确定何时一个函数较小并且最好内联它,或者什么时候该函数较大并且更好地将其分离并使其成为一个真正的函数,以生成最快的代码(如果已设置要生成的代码)最快的代码).您看不到它,所以您猜到了而且可能猜错了.

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.

您拒绝,但我要说的是:我强烈建议您在决定执行此操作之前,先配置文件您的代码,我几乎可以保证这样做不值得您花时间.编译器(其中大多数是近AI级别的智能程序)可能会生成作为具有常规函数调用的更快或更快的代码,更不用说维护方面了.

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.

这篇关于与调用函数相比,goto语句有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆