当前位置:网站首页>Brief description of compiler optimization level

Brief description of compiler optimization level

2022-07-06 15:09:00 eric_ [email protected]

Compile optimization , It will make the compiler try at the expense of compilation time and the ability to debug the program , To improve performance or code size ;
Reasonable optimization level , Can be in code volume 、 Executive performance 、RAM Get a balance in share !
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

1、 Optimization level description

GCC The optimization level is -O0、-O1、-O2、-O3、-Og、-Os、-Ofast
 Insert picture description here

Summarized below :

Optimization level explain remarks
-O0 Turn off all optimizations Code space is big , Inefficient execution -O1 The basic optimization level compiler does not spend too much compilation time , Try to generate faster 、 Smaller code
-O2O1 Upgraded version , Recommended optimization level The compiler tries to improve code performance , Without increasing the volume and taking up too much compilation time
-O3 The most dangerous optimization level It will prolong the code compilation time , Generate larger volume 、 More memory consuming binary files , Greatly increase the probability of compilation failure and unpredictable program behavior , Do more harm than good
-OgO1 On the basis of , Eliminate optimizations that affect debugging If the final purpose is to debug the program , You can use this parameter . But this parameter alone is not enough , This parameter just tells the compiler , The compiled code should not affect debugging , But the generation of debugging information still depends on -g Parametric
-OsO2 On the basis of , Further optimize code size Eliminate optimizations that will increase the size of the final executable , If you want a smaller executable , This parameter can be selected .
-Ofast Optimize to the point that destroys standard compliance ( Equivalent to -O3 -ffast-math ) Is in -O3 On the basis of , Added some unconventional optimizations , These optimizations are made by breaking some international standards ( For example, the implementation standards of some mathematical functions ) To achieve , Therefore, this parameter is generally not recommended .

give an example :
 Insert picture description here
 Insert picture description here

2、MDK Optimization level modification

  • Set the optimization level globally
     Insert picture description here

  • Set the optimization level locally

Usually , As the code structure is complex , The addition of third-party components , The overall optimization level can no longer meet the actual use scenarios ;
MDK It can be applied to a single file , Or set the optimization level separately for a single group of files .

 Insert picture description here

  • Set the optimization level in the code

In the code , Set the optimization level separately for a function

https://www.keil.com/support/man/docs/armcc/armcc_chr1359124988971.htm
 Insert picture description here

AC5 Setting method in :

#pragma push
#pragma O1
void function(void){
    
    ...                 // Optimized at O0
}
#pragma pop

AC6 Setting method in :

void function(void) _attribute__((optnone))
{
    
    ...   // Optimized none
}

Reference resources :
https://www.cnblogs.com/armfly/p/15605027.html

原网站

版权声明
本文为[eric_ [email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131323057796.html