直接上程序:

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. tomcat报警告 An attempt was made to authenticate the locked user

    有好多这样的警报怪怪的,一分钟抛一次,大概抛了10分钟,停止 有 Anattempt was made to authenticate the locked user "root" ...

  2. USACO Section 4.2: Drainage Ditches

    最大流的模板题 /* ID: yingzho1 LANG: C++ TASK: ditch */ #include <iostream> #include <fstream> ...

  3. MVC 的知识

    MVC   的知识 下载地址: 1. NET Framework4下载地址: http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyI ...

  4. Linux内核等待队列

    在Linux驱动程序设计中,可以使用等待队列来实现进程的阻塞,等待队列可看作保存进程的容器,在阻塞进程时,将进程放入等待队列,当唤醒进程时,从等待等列中取出进程. Linux 2.6内核提供了如下关于 ...

  5. Uubuntu 14.04 LTS反编译apk

    使用apktool反编译apk 1.安装apktool apktool是Google提供的APK编译工具,能够反编译及回编译apk,需要Java环境的支持(在此不再赘述Java的安装与配置,详见< ...

  6. Eclipse 下如何引用另一个项目的资源文件

    为什么要这么做?可参考:Eclipse 下如何引用另一个项目的Java文件 下面直接说下步骤:(项目A 引用 项目B的资源文件) 1.右键 项目A,点击菜单 Properties 2.在弹出的框中,点 ...

  7. YTU 2602: 熟悉题型——类设计( 矩形类定义【C++】)

    2602: 熟悉题型--类设计( 矩形类定义[C++]) 时间限制: 1 Sec  内存限制: 128 MB 提交: 183  解决: 119 题目描述 定义一个矩形类,数据成员包括左下角和右上角坐标 ...

  8. Java中什么时候使用构造方法

    JAVA是面向对象的语言,面向对象不是这么直接简单,它的设计思想就是要代码重用.即我以前干过类似的事,那么我找出以前可以用到的代码,完成一部分.以前没有的我重新写.这样就有了类.有了类,就是有了可以重 ...

  9. python 统计文本文件的行数

    num_lines = sum(1 for line in open(input_file_name))

  10. UESTC 1074 秋实大哥搞算数 栈模拟

    秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit S ...