直接上程序:

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. [原]素数筛法【Sieve Of Eratosthenes + Sieve Of Euler】

    拖了有段时间,今天来总结下两个常用的素数筛法: 1.sieve of Eratosthenes[埃氏筛法] 这是最简单朴素的素数筛法了,根据wikipedia,时间复杂度为 ,空间复杂度为O(n). ...

  2. YTU 2610: A改错题--体检情况分析

    2610: A改错题--体检情况分析 时间限制: 1 Sec  内存限制: 128 MB 提交: 233  解决: 161 题目描述 注:本题只需要提交标记为修改部分之间的代码,请按照C++方式提交. ...

  3. Mybatis 插入与批量插入以及多参数批量删除

    实体类: import java.io.Serializable; public class AttachmentTable implements Serializable { private sta ...

  4. WCF常见异常-The maximum string content length quota (8192) has been exceeded while reading XML data

    异常信息:The maximum string content length quota (8192) has been exceeded while reading XML data 问题:调用第三 ...

  5. LA 4670 (AC自动机 模板题) Dominating Patterns

    AC自动机大名叫Aho-Corasick Automata,不知道的还以为是能自动AC的呢,虽然它确实能帮你AC一些题目.=_=|| AC自动机看了好几天了,作用就是多个模式串在文本串上的匹配. 因为 ...

  6. iPad中控制器view的width和height

    一.iPad中控制器view的width和height 1> 规律 * width 是宽高中最小的那个值 * height 是宽高中最大的那个值 2> 举例(比如窗口根控制器的view,有 ...

  7. unity3d 破解安装

    1.下载破解程序,执行生成unity_v4.x.ulf文件 2.断网 3.执行unity客户端,load该lisence文件即可 注意:安装unity客户端完成后,未破解,切记别打开unity客户端 

  8. jquery 获取和设置 select下拉框的值(转手册)

    ##实例应用中遇到的问题 //在某事件响应的应用中设置select选中项,前两种情况的设置不生效,使用了最后一种用法才生效的 //$("#select_time").find(&q ...

  9. php 获取301 302的真实地址

    function getRealURL($url){ $header = get_headers($url,1); if (strpos($header[0],'301') || strpos($he ...

  10. python练习程序(c100经典例10)

    题目: 打印楼梯,同时在楼梯上方打印两个笑脸. print '..' for i in range(1,9): print '**' for j in range(1,i+1): print ' ',