从 GCC 获取优化的源代码

发布于 2024-12-21 23:58:06 字数 186 浏览 6 评论 0原文

我的任务是创建优化的 C++ 源代码并将其交给朋友进行编译。这意味着,我不控制最终的编译,我只是编写C++程序的源代码。

我知道,a 可以在编译过程中使用 GCC 的 -O1 (和 -O2 等)选项进行优化。但是我怎样才能得到这个优化的源代码而不是编译后的程序呢?我无法配置我朋友的编译器的参数,这就是为什么我需要在我这边制作一个好的源代码。

I have a task to create optimized C++ source code and give it to friend for compilation. It means, that I do not control the final compilation, I just write the source code of C++ program.

I know, that a can make optimization during compilation with -O1 (and -O2 and others) options of GCC. But how can I get this optimized source code instead of compiled program? I am not able to configure parameters of my friend's compiler, that is why I need to make a good source on my side.

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

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

发布评论

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

评论(3

格子衫的從容 2024-12-28 23:58:06

GCC 执行的优化是低级别的,这意味着您不会再次获得 C++ 代码,而是在最好的情况下获得汇编代码。但你将无法转换它或其他东西。

总之:在代码级别而不是对象级别优化源代码

The optimizations performed by GCC are low level, that means you won't get C++ code again but assembly code in best case. But you won't be able to convert it or something.

In sum: Optimize the source code on code level, not on object level.

许你一世情深 2024-12-28 23:58:06

您可以要求GCC在各个“阶段”转储内部(Gimple,...)表示 。 GCC 的中端由数百个通道组成,您可以要求 GCC 转储它们,使用 参数,例如 -fdump-tree-all-fdump-gimple-all;请注意,一次编译可能会获得数百个转储文件!

然而,GCC 内部表示的级别相当低,您不应期望在不阅读大量材料的情况下理解它们。

我提到的转储选项对于那些在 GCC 内部工作的人来说非常有用,或者通过用 C 编码的插件或用 MELT(一种扩展 GCC 的高级领域特定语言)。我不确定它们对你的朋友是否非常有用。但是,它们可以帮助您了解优化过程会执行大量复杂的处理。

并且不要忘记过早的优化是邪恶的:您应该首先让您的程序正确运行,然后对其进行基准测试和分析,最后优化值得您努力的几个部分。您可能无法写出正确的 &高效的程序,无需亲自测试和运行,然后再将其交给您的朋友。

You could ask GCC to dump its internal (Gimple, ...) representations, at various "stages". The middle-end of GCC is made of hundreds of passes, and you could ask GCC to dump them, with arguments like -fdump-tree-all or -fdump-gimple-all; beware that you can get hundreds of dump files for a single compilation!

However, GCC internal representations are quite low level, and you should not expect to understand them without reading a lot of material.

The dump options I am mentionning are mostly useful to those working inside GCC, or extending it thru plugins coded in C or extensions coded in MELT (a high-level domain specific language to extend GCC). I am not sure they will be very useful to your friend. However, they can be useful to make you understand that optimization passes do a lot of complex processing.

And don't forget that premature optimization is evil : you should first make your program run correctly, then benchmark and profile it, at last optimize the few parts worth of your efforts. You probably won't be able to write correct & efficient programs without testing and running them yourself, before giving them to your friend.

静水深流 2024-12-28 23:58:06

简单 - 选择尽可能最好的算法,让优化器处理其余的事情。

优化源代码与优化二进制文件不同。 优化源代码,编译器将优化二进制文件。

除了算法选择之外,您还需要进行一些分析。当然,有些做法可以加快代码速度,但有些做法会降低代码的可读性。仅在必要时以及在测量之后进行优化。

Easy - choose the best algorithm possible, let the rest be handled by the optimizer.

Optimizing the source code is different than optimizing the binary. You optimize the source code, the compiler will optimize the binary.

For anything more than algorithm choice, you'll need to do some profiling. Sure, there are practices that can speed up code speed, but some make the code less readable. Only optimize when you have to, and after you measure.

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