Introduction to C and C++ compiler optimization

  • 2020-05-30 20:51:35
  • OfStack

0. gcc -o

The optimization of gcc-o is still mechanical and taken for granted. Only when you have a deep understanding of computer systems and a deep understanding of programming languages can you write optimized code.

An introduction to the gcc optimization level under Linux

・ gcc - o0 & # 8658; Does not provide any optimization;

・ gcc - o1 & # 8658; The most basic optimization mainly optimizes the branch, expression, constant and so on of the code. The compiler will make the code shorter in a short time, so that the volume will become smaller and the memory occupancy will be reduced, which will be faster when the operating system performs memory scheduling.

· however, there are no absolute advantages. When a huge program is broken down and subdivided, the memory consumption will increase greatly. Since most of today's systems are multi-threaded, there will be jam and reaction delay.

· large project codes are not suitable for gcc-o1 optimization;

· gcc-o2: it is an advance of o1, which will be divided more strictly on the basis of o1. The most important thing is the utility of registers.

1. volatile keyword

The volatile keyword prevents excessive compiler optimization by doing two things:

· prevents the compiler from caching 1 variable into a register in order to speed it up without writing it back;

· prevent the compiler from adjusting the order of instructions for manipulating volatile variables;

2. register keyword

The way to place code in registers is to use register to modify variables for variables that are called frequently.


That's the end of this article on C/C++ compiler optimization. Welcome to refer to the site of other related topics, have any questions can give us a message at any time, this site will timely reply to you.

Related articles: