直接上程序:

setjmp和longjmp是处理函数嵌套调用的,goto语句不能跨越函数,所以不选择goto。

#include <setjmp.h>
int setjmp(jmp_buf env); //返回值:若第一次直接调用则直接返回0,若从longjmp调用则返回下面的val
void longjmp(jmp_buf env, int val);

对程序进行不带优化编译:

[henry@localhost c]$ gcc -g youhua.c -o youhua

对程序进行带优化的编译:

[henry@localhost c]$ gcc -g -O youhua.c -o youhua_after

对比上面结果可以看到,全局、静态、volatile变量不受优化的影响。

  • 不进行优化时,上面定义的5个变量包括register变量都直接从内存中取值。
  • 进行优化后,register变量和局部变量gcc都是从寄存器中取的值。 

gcc都做了什么优化呢?首先可以看到变量从内存取值优化到从寄存器取值。一下是manual的部分翻译。

gcc有几个优化等级:

O0,O1,O2,O3

-O0表示没有优化,-O1为缺省值,-O3优化级别最高

'-O '
'-O1 '
Optimize. Optimizing compilation takes somewhat more time, and a
lot more memory for a large function. With `-O ', the compiler tries to reduce code size and execution
time, without performing any optimizations that take a great deal ##编译器试着减少代码段的大小和代码执行时间,如果没有执行一些
of compilation time. 优化结果将花费大量编译时间。 `-O ' turns on the following optimization flags:
-fdefer-pop 延迟到必要时在函数栈种pop参数
-fdelayed-branch
-fguess-branch-probability
-fcprop-registers
-floop-optimize
-fif-conversion
-fif-conversion2
-ftree-ccp
-ftree-dce
-ftree-dominator-opts
-ftree-dse
-ftree-ter
-ftree-lrs
-ftree-sra
-ftree-copyrename
-ftree-fre
-ftree-ch
-funit-at-a-time
-fmerge-constants `-O ' also turns on `-fomit-frame-pointer ' on machines where doing ## ’-O‘也打开-fomit-frame-pointer标志当机器                    so does not interfere with debugging. 这样做不会影响干涉调试。          `-O ' doesn 't turn on `-ftree-sra ' for the Ada compiler. This
          option must be explicitly specified on the command line to be enabled for the Ada compiler.`-O2 ' 
 `-O2 '  
Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff.The compiler does not perform loop unrolling or function inlining when you specify `-O2 '.As compared to `-O ',this option increases both compilation time and the performance of the generated code.
  进一步的优化。GCC会支持所有不涉及时间空间交换的所有支持的优化选项。当你加入-o2选项时,编译器不会进行循环展开和函数内联。与-O选项相比,这个选项会增加编辑时间和合成码的性能。
   `-O2' turns on all optimization flags specified by `-O'. It also turns on the following optimization flags:
    -fthread-jumps    
    -fcrossjumping    
    -foptimize-sibling-calls    
    -fcse-follow-jumps
    -fcse-skip-blocks    
    -fgcse
    -fgcse-lm       
    -fexpensive-optimizations    
    -fstrength-reduce    
    -frerun-cse-after-loop
    -frerun-loop-opt    
    -fcaller-saves    
    -fpeephole2    
    -fschedule-insns      
    -fschedule-insns2    
    -fsched-interblock
    -fsched-spec    
    -fregmove    
    -fstrict-aliasing    
    -fdelete-null-pointer-checks    
    -freorder-blocks
    -freorder-functions    
    -falign-functions
    -falign-jumps    
    -falign-loops
    -falign-labels    
    -ftree-vrp    
    -ftree-pre 
    Please note the warning under `-fgcse' about invoking `-O2' on programs that use computed gotos. 
 
  
    `-O3' 
    Optimize yet more.`-O3 ' turns on all optimizations specified by `-O2' and also turns on the `-finline-functions ',`-funswitch-loops' and `-fgcse-after-reload' options. 
   再一次的优化,-O3选项会添加所有-O2中添加的选项,并且添加`-finline-functions ',`-funswitch-loops' and `-fgcse-after-reload' 这三个选项
    `-O0' 
    Do not optimize.This is the default.
    
   
   -Os相当于-O2.5。是使用了所有-O2的优化选项,但又不缩减代码尺寸的方法。
   详细的说明如下:
Level 2.5 (-Os)
 
The special optimization level (-Os or size) enables all -O2 optimizations that do not increase code size; it puts the emphasis on size over speed. This includes all second-level optimizations, except for the alignment optimizations. The alignment optimizations skip space to align functions, loops, jumps and labels to an address that is a multiple of a power of two, in an architecture-dependent manner. Skipping to these boundaries can increase performance as well as the size of the resulting code and data spaces; therefore, these particular optimizations are disabled. The size optimization level is enabled as:
   -Os这个特殊的优化等级,能够实现-O2的全部不增加代码段大小优化,他强调程序的大小而不是程序的运行速度,他包含了所有第二等级的优化,除了对齐优化,这些对齐优化在体系结构的依赖性的程序中,跳过一些线性结构,循环,跳转和标签的空间,到一个指数为2的多项式和的地址。跳过这些界限可以提高性能,以及由此产生的代码和数据空间的大小,因此,这些特定的优化被禁用。

完!

参考:·[1]

apue

gcc都做了什么优化的更多相关文章

  1. 【webapp的优化整理】要做移动前端优化的朋友进来看看吧

    单页or多页 本文仅代表个人观点,不足请见谅,欢迎赐教. webapp 小钗从事单页相关的开发一年有余,期间无比的推崇webapp的网站模式,也整理了很多移动开发的知识点,但是现在回过头来看,weba ...

  2. 面试:做过sql优化吗?

    近来面试找工作经常会遇见这种问题: 做过数据库优化吗?大数据量基础过吗?系统反应慢怎么查询? 这咱也没背过啊,面试还老问,现在的网站主要的压力都来自于数据库,频繁的数据库访问经常会使系统瘫痪,这样就需 ...

  3. [转帖]支撑双11每秒17.5万单事务 阿里巴巴对JVM都做了些什么?

    支撑双11每秒17.5万单事务 阿里巴巴对JVM都做了些什么? https://mp.weixin.qq.com/s?__biz=MzA3OTg5NjcyMg==&mid=2661671930 ...

  4. OGG-Oracle 集成模式抽取进程,REGISTER DATABASE都做了什么?

    一.学习目标 有同事问OGG技术问题,OGG软件,在oracle数据库中,集成模式抽取进程REGISTER DATABASE,都做了什么操作? 有什么风险? 并且提到了一个抽取进程注册,在瞬时间并发占 ...

  5. 深入探索Glide图片加载框架:做了哪些优化?如何管理生命周期?怎么做大图加载?

    前言 Glide可以说是最常用的图片加载框架了,Glide链式调用使用方便,性能上也可以满足大多数场景的使用,Glide源码与原理也是面试中的常客. 但是Glide的源码内容比较多,想要学习它的源码往 ...

  6. configure, make, make install都做了什么

    1. 我的理解./configure:  确保接下来的make以及make install所依赖的文件没有问题make:  build编译连接生成可执行程序make install: 将编译好的可执行 ...

  7. 从架构演进的角度聊聊Spring Cloud都做了些什么?

    Spring Cloud作为一套微服务治理的框架,几乎考虑到了微服务治理的方方面面,之前也写过一些关于Spring Cloud文章,主要偏重各组件的使用,本次分享主要解答这两个问题:Spring Cl ...

  8. Java对象的创建 —— new之后JVM都做了什么?

    Java对象创建过程 1. 类加载检查 虚拟机遇到一条new指令时,首先将去检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并且检查这个符号引用代表的类是否已经被加载.解析和初始化过.如果没 ...

  9. 从架构演进的角度聊聊Spring Cloud都做了些什么

    1.从架构演进的角度聊聊Spring Cloud都做了些什么?2.中小型互联网公司微服务实践-经验和教训3.Spring Cloud在国内中小型公司能用起来吗?

随机推荐

  1. Win API 内存整理

    记得我的笔记本上曾经安装了一款名为内存整理大师的软件,当时觉得挺好用而且挺NB的,就是导致开机启动有点慢. 当时我就在想,内存整理是怎么实现的?不过那是水平实在是不怎么样,估计连windows程序的消 ...

  2. Xcode使用版本

    Xcode6中创建分类.协议等文件的方法 Xcode 5.1.1 与 Xcode 6.0.1 的共存之路 http://jingyan.baidu.com/article/1612d500457df1 ...

  3. MTK6577+Android环境变量

    1. 环境变量机器对应的路径 $project = hsimobile77_ics2 $platform=mt6577 $(PRODUCT_OUT)=\out\target\product\$proj ...

  4. leetcode:Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  5. 《OD学hadoop》Linux基础

    一.Linux基本环境 1. Linux常见版本及VMware虚拟机安装Linux系统 2. 虚拟机网络配置(IP地址.主机名.防火墙) 3. 文件基本命令操作 4. 四大远程连接工具使用 二.Lin ...

  6. the specified child alread has a parent

    用 TestFragment   extends  Fragment     @Override     public  View onCreateView(LayoutInflater inflat ...

  7. POJ 1988 Cube Stacking

    题意:有编号为1~N的N个小木块,有两种操作 M x y 将木块x所在的堆放到木块y所在的堆的上面 C x 询问木块x下面有多少块木块 代码巧妙就巧妙在GetParent函数中在进行路径压缩的同时,也 ...

  8. Servlet的延迟加载和预加载

    我们什么时候使用了延迟加载呢? 先从hibernate引入这个概念吧. hibernate使用lazy属性设置延迟加载,load方法会使用延迟加载. 举个例子: 一个学生有多部手机,如果使用了延迟加载 ...

  9. iOS中第三方框架刷新

    0.先加入主头文件 #import "MJRefresh.h" 1.添加下拉刷新 MJRefreshHeaderView *header = [MJRefreshHeaderVie ...

  10. 浅析extendedLayout, automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars

    参考文章: http://stackoverflow.com/questions/18798792/explaining-difference-between-automaticallyadjusts ...