C++ :编译器以多少种方式优化我们的代码?

发布于 2024-10-06 11:04:53 字数 179 浏览 1 评论 0原文

我想知道编译器可以/确实优化用 C++ 编写的代码的所有可能方法(或至少是流行的方法)?我还想知道优化是如何完成的(在每种情况下)!

到目前为止,我知道两个优化,即。空基优化 (EBO) 和返回值优化 (RVO)。还有什么?我听说过“const”优化、“未使用的变量”优化。这些是什么?

.

I would like to know all the possible ways (or atleast the popular ones) in which compilers can/do optimize away our code written in C++? I also would like to know how exactly the optimization is done (in each case)!

So far, I'm aware of two optimizations, viz. Empty Base Optimization (EBO) and Return Value Optimization (RVO). What else are there? I've heard of "const" optimization,"unused variable" optimization. What are they?

.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离旧人 2024-10-13 11:04:53

所有可能的方式?你肯定是在开玩笑。为此,请回顾多年的编译器研究和实践。

有关具体示例,请在此处查找每个选项: http://gcc.gnu .org/onlinedocs/gcc/Optimize-Options.html

All possible ways? Surely you're joking. For that, look through years of compiler research and practice.

For concrete examples, look up each of the options here: http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

萌︼了一个春 2024-10-13 11:04:53

来自标准文档,第 1.9 节

4) 该规定有时被称为“假设”规则,因为实施可以自由地忽略本国际标准的任何要求
只要结果就像满足了要求一样,只要可以从程序的可观察行为确定即可。例如,
如果实际的实现可以推断出其值未被使用并且没有影响表达式的副作用,则不需要计算表达式的一部分
产生了程序的可观察行为。


因此,实际上符合标准的编译器可以执行任何类型的优化,只要它产生所需的结果即可。

From Standard docs., Section 1.9,

4) This provision is sometimes called the “as-if” rule, because an implementation is free to disregard any requirement of this International Standard
as long as the result is as if the requirement had been obeyed
, as far as can be determined from the observable behavior of the program. For instance,
an actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no side effects affecting the
observable behavior of the program are produced.

So actually the standard compliant compiler can perform any kind of optimizations as long as it produces the desired result.

梦里南柯 2024-10-13 11:04:53

范围之广令人难以置信,因为有很多优化,而且编译器编写者总是想出更多。它们有很多,一些针对运行时间进行优化,另一些针对二进制大小进行优化。许多也不是专门针对 C++ 的,通用编译器优化技术是为许多不同语言的许多编译器/解释器实现的。

只是少数:

更多信息:

Incredibly broad, because there are many optimizations and compiler writers are always thinking up more. There are tons of them, some optimizing for run time, others optimizing for binary size. Many aren't specifically C++ either, the general compiler optimization techniques are implemented for many compilers/interpreters for many different languages.

Just a handful:

More info:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文