简体   繁体   English

你可以在你的c源代码中手动执行所有gcc优化(-O2,-O3)吗?

[英]Can you do all gcc optimizations (-O2, -O3) manually in your c source code?

In my class project, my project is set to use gcc's optimization level of -O0 (no optimizations) and we are not allowed to change it for the final submission. 在我的类项目中,我的项目设置为使用gcc的优化级别-O0(无优化),我们不允许在最终提交时更改它。

I tested my code using -O2 and got around a 2x speedup of my entire program. 我使用-O2测试了我的代码,并获得了整个程序的2倍加速。 So I was wondering, is it possible to go through each optimization that -O2 does, and manually do those optimizations in my code? 所以我想知道,是否有可能完成-O2所做的每个优化,并在我的代码中手动执行这些优化? Or are some of the -O2 optimizations internal to the stack, frame, machine/assembly, etc, thus disallowing me, the programmer, from manually making those optimizations in my source code (If that makes sense) 或者是堆栈,框架,机器/程序集等内部的一些-O2优化,因此不允许程序员在我的源代码中手动进行那些优化(如果这样做有意义的话)

Is it possible to go through each optimization that -O2 does, and manually do those optimizations in my code? 是否可以通过-O2执行的每个优化,并在我的代码中手动执行这些优化?

No. Many of the optimizations performed by the compiler cannot be represented in C. Some of these include: 否。编译器执行的许多优化都不能用C表示。其中一些包括:

  • Disabling the frame pointer 禁用帧指针
  • Removing unnecessary register saves/restores at the beginning and end of a function 在函数的开头和结尾删除不必要的寄存器保存/恢复
  • "Peephole" optimizations on the assembly, such as removing redundant moves, loads, or stores 装配上的“窥视孔”优化,例如移除冗余移动,装载或存储
  • Inserting no-ops to align loops to specific address boundaries (typically 16 bytes) 插入no-ops以将循环对齐到特定的地址边界(通常为16个字节)

This isn't to say that all of the optimizations performed by the compiler are untranslatable, of course -- merely that some of them are. 这并不是说编译器执行的所有优化都是不可翻译的 - 当然 - 仅仅是其中一些优化。

Yes, but that's the same as building your own 8086-class microprocessor in Minecraft — not worth your time and effort. 是的,但这与在Minecraft中构建自己的8086级微处理器相同 - 不值得花时间和精力。 And yes, many of those optimizations involve stuff below the language level of abstraction. 是的,这些优化中的许多涉及低于抽象语言级别的东西。 Your professor might have unknown-to-you reasons for wanting an unoptimized executable. 你的教授可能不知道你想要一个未经优化的可执行文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM