Discussion on gcc optimization level in Linux environment

  • 2020-06-15 10:58:17
  • OfStack

Code optimization is a very complicated and very important question, for many years the author linux c development experience optimization is usually divided into two aspects, one is for optimization, which is based on programming experience using a simple data structure function and so on to reduce the burden of the compiler, the optimization model system at 2 is adopted, namely gcc - o series, below I will briefly 1 under the optimization process, and at all levels.

gcc - o1 o1 first with a o0, that is does not provide any optimization, almost do not use in the project, and o1 use is very broad, o1 is the most basic optimization, the main branch of the code, expressions, constants for optimization, the compiler will be in a relatively short time under the code becomes more short, the volume will become smaller, less memory usage, while the operating system for memory scheduling will be faster. However, there is no absolute advantage to things. When a large program is broken down and subdivided, the memory usage will be greatly increased. Since most of today's systems are multi-threaded, there will be delays and latency.

The optimization level of gcc-o2 is a step up from o1, with more rigorous subdivision and, most importantly, the use of registers. Register is an important part in cpu, in addition to the unit and controller, computer, as the name implies, want to undertake a variety of complex calculations, because cpu faster, so the calculation of the intermediate results will be stored in registers, it can greatly improve the efficiency of the system, but the register expensive, quantity is limited, so the program will not on the register as a 1, the other one will be code on the register of way is to use register modify variables, suitable for frequently called variables.

The gcc-o3 optimization is a very powerful optimization because the compiler makes predictions for each layer of the loop so that the loop can be broken up, improving execution efficiency. The compiler will also try to replace an unknown value with an existing value, and will also replace multiplication with addition, because of the nature of the arithmetic, multiplication by 10 minutes is complex and time-consuming. Of course, the most obvious disadvantage of o3 is that o3 may have errors because it tries to predict the direction of the program, leading to errors and irreversible direction of the program. Therefore, general o3 is not recommended.
These are the three levels of optimization, for optimization, system optimization or mechanical, programmers have a deep understanding of the language, clever algorithm may be more meaningful.

conclusion


Related articles: