博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
optimization blocks (csapp chapter 5.1)
阅读量:7128 次
发布时间:2019-06-28

本文共 2812 字,大约阅读时间需要 9 分钟。

p_511

编译器在没有指示下,会做‘ safe optimization',因此有些优化在没有参数的指示下是不会对代码做优化的,故在程序中应该避免某一类代码,因为它们妨碍了编译器做优化。

optimization blocks: aspects of programs that can severely limit the opportunities for a compiler to generate optimized code;

两类optimization blocks:

1、memory aliasing

 pointers may generate the same memory location is known as memory aliasing. In performing only safe optimizations, the compiler must assume that different pointers may be aliased, limiting the set of possible optimizations.

// from cmu/* Sum rows is of n X n matrix a   and store in vector b  */void sum_rows1(int  *a, int  *b, int n) {    int i, j;    for (i = 0; i < n; i++) {    b[i] = 0;    for (j = 0; j < n; j++)        b[i] += a[i*n + j];    }}/*如果我们调用时为  int A[9];  int* B = A + 3;  sum_rows1(A, B);   如果编译器将其优化成如下类的形式,显然有背原意    void sum_rows2(int *a, int *b, int n) {
    int i, j;     for (i = 0; i < n; i++) {
    double val = 0;     for (j = 0; j < n; j++)         val += a[i*n + j];          b[i] = val;     } }*/ /*简化了sum_rows1:.L4: movl %ebp, %ecx movl $0, (%edx,%ebp,4) movl $0, %eax .L3: movl (%esi,%eax,4), %ebx ;(%esi, %eax, 4) : &a[i*n + j] addl %ebx, (%edx,%ecx,4) ;(%edx, %ecx, 4) : &b[i] addl $1, %eax ;%eax : j cmpl %edi, %eax ;%edi : n jne .L3 addl $1, %ebp ;%ebp : i addl (%esp), %esi cmpl %edi, %ebp jne .L4 */
//书上例子//当xp = yp时,twiddle1 与 twiddle2显然不同,因此编译器不会做一些优化,以免将twiddle1优化成与twiddle2相同的功能函数void twiddle1(int *xp, int* yp){     *xp += *yp;     *xp += *yp;   }void twiddle2(int *xp, int* yp){      *xp += 2* *yp;}

2、procedure calls

    Most compilers do not try to determine whether a function is free of side effects and hence is a candidate for optimizations.Instead, the compiler assumes the worst case and leaves function call intact(原封不动)

//经典(from cmu)void lower(char* s){      int i = 0;      for (i = 0; i < strlen(s); i++)            if(s[i] >= 'A' && s[i] <= 'Z')                  s[i] -= ('A' - 'a');      }//编译器为何不把strlen(s)提取出来,作为一个临时量,这样就可以减少函数调用了?//Why couldn’t compiler move strlen out of  inner loop?//      (1)Procedure may have side effects//                 Alters global state each time called//       (2)Function may not return same value for given arguments//                  Depends on other parts of global state//                  Procedure lower could interact with strlen//Warning:          (1)Compiler treats procedure call as a black box          (2)Weak optimizations near them
//书上的例子int f(void);int func1(void){     return f() + f() + f() + f();//procedure call不可优化成4*f(), 否则就错了}int func2(void){     return 4 * f();   }int counter = 0;int f(void){     return counter++;}

 

转载地址:http://dnhel.baihongyu.com/

你可能感兴趣的文章
复习下C 链表操作(单向链表)
查看>>
栈结构的经典算法题
查看>>
Contoso 大学 - 7 – 处理并发
查看>>
好命的姑娘?命运取决于你现在在干什么!
查看>>
atitit.GUI图片非规则按钮跟动态图片切换的实现模式总结java .net c# c++ web html js...
查看>>
分享一下spark streaming与flume集成的scala代码。
查看>>
关于HOSTS设置不生效的解决小方法
查看>>
无法打开物理文件mdf,操作系统错误 5:&quot;5(拒绝訪问。)&quot;
查看>>
Dynamic CRM 2013学习笔记(二十三)CRM JS智能提示(CRM 相关的方法、属性以及页面字段),及发布前调试...
查看>>
ecside使用笔记(1)
查看>>
eclipse+webservice开发实例
查看>>
js undefined易错分析
查看>>
程序员必须知道的几个Git代码托管平台(转)
查看>>
PHP 二维数组根据相同的值进行合并
查看>>
微信JS-SDK使用权限签名算法的服务端实现(.net版本)
查看>>
windows下ruby安装环境配置
查看>>
Wndows 主进程(Rundll32)已停止工作
查看>>
C#的百度地图开发(一)发起HTTP请求
查看>>
用12306购票所想到的(改善的地方)
查看>>
Java设计模式(1)工厂模式(Factory模式)
查看>>