goto对C ++编译器优化的影响 [英] effect of goto on C++ compiler optimization

查看:168
本文介绍了goto对C ++编译器优化的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用现代C ++编译器使用 goto 会带来哪些性能优势或处罚?

What are the performance benefits or penalties of using goto with a modern C++ compiler?

我正在编写一个C ++代码生成器,使用 goto 将更容易编写。 没有人会触摸生成的C ++文件,所以不要全部goto是坏对我。作为一个好处,它们保存了临时变量的使用。

I am writing a C++ code generator and use of goto will make it easier to write. No one will touch the resulting C++ files so don't get all "goto is bad" on me. As a benefit, they save the use of temporary variables.

我想知道,从纯编译器优化的角度来看,goto对编译器的优化器有什么结果?与使用临时/标记相比,它是否会使代码更快,更慢,或者一般来说没有更改

I was wondering, from a purely compiler optimization perspective, the result that goto has on the compiler's optimizer? Does it make code faster, slower, or generally no change in performance compared to using temporaries / flags.

推荐答案

受影响的编译器部分使用流程图。用于创建特定流程图的语法通常是不相关的 - 如果你使用 goto 创建类似 $ c>而不是实际的语句,它不会影响优化器的所有(到那一点,生成流程图的语法将很快消失)。

The part of a compiler that would be affected works with a flow graph. The syntax you use to create a particular flow graph will normally be irrelevant -- if you create something like a while loop using a goto instead of an actual while statement, it's not going to affect the optimizer at all (by that point, the syntax that produced the flow graph will be long gone).

但是,可以使用 goto 生成不能由任何正常的流控制语句(循环,开关等)在这种情况下,你可能会产生一个不可约的流程图,当/如果你这样做,这往往会限制编译器优化代码的能力。

It is possible, however, to produce a flow graph with gotos that couldn't be produced by any normal flow control statements (loops, switch, etc.) In such a case, you may produce an irreducible flow graph, and when/if you do, that will often limit the ability of the compiler to optimize the code.

换句话说,如果(例如)您使用,等,并将其转换为 goto 保留了相同的结构,几乎任何合理的现代编译器可能产生基本相同的代码。然而,如果你使用 goto 产生混乱的意大利面条像我在几十年前看过的FORTRAN,那么编译器可能不能做很多。

In other words, if (for example) you took code that was written with normal for, while, switch, etc., and converted it to use goto in every case, but retained the same structure, almost any reasonably modern compiler would probably produce essentially identical code either way. If, however, you use gotos to produce the mess of spaghetti like much of the FORTRAN I had to look at decades ago, then the compiler probably won't be able to do much with it.

这篇关于goto对C ++编译器优化的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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