首页 > 其他分享 >CS:APP--Chapter05 : optimizing program performance (part 1)

CS:APP--Chapter05 : optimizing program performance (part 1)

时间:2023-01-08 02:33:05浏览次数:47  
标签:chapter function optimizing code -- optimization program compiler

标签(空格分隔): CS:APP


prologue

The primative object of a programmer is make it run correctly even fast. Not only do we ensure others can make sense of it, but also others can understand the code during review and when some modifications are needed.

Several types of program optimization are necessary :

  1. select an appropriate set of data structure and algorithm
  2. write code in a way that we assist compiler to turn source code into an efficient executable code
  3. parallelism ( detailen it in chapter 12 )

This chapter focus on one ultimate goal - how to make code run faster via several different types of program optimization. Eliminating the unnecessary work that has nothing to do with processor and executing the divided work in parallel that highlt depend on the architecture of processor make our code run faster. For increasing the degree of parallelism with which we execute our code, more information will be discussed in chapter 12.

A good way of gain an better understanding of optimization is looking into the assembly language with the optimzation level Og and O1 and even higher.

1. capabilities and limtation of optimizing compiler

Here, compiler with a growing ability of optimization would not optimize code in a aggresive and radical way due to its constraints.

1.1 memory aliasing

In many cases, the probability of referring to a identical memory makes compiler runs with a safe optimization.

A example is proposed on page 535 that adding two numbers twice is for a factor of 4 to one variable, which runs same as 4 times it with less operations at assembly-language level. But compile cannot optimize first directly to the second case, because compiler doesn't ensure whether or not they refer to the identical memory leading to the erroneous result.

1.2 function calls

another optimization block here is optimizing multiple calls of one function to less times of call for the function may leading to side effect here. If there is a global variable, it must be modified several times to change program behavior.

In this case, compile assume the worst case and just leaves the function calss intact(unchanged).

Even though many types of optimization exploit program to its full extent, the constraints of compilers limits the set of optimization.

2. expressing program performance

Like the metric throughout and latency in last chapter, a metric "cycles per element" is introduced here, abbreviated by CPM and Especially for loop behavior.

The time required by the precudure is characterized as a constant plus a factor proportional to the number of elements processed.

-> least squares fit to find the expression for example: 60+35n

标签:chapter,function,optimizing,code,--,optimization,program,compiler
From: https://www.cnblogs.com/UQ-44636346/p/17034019.html

相关文章

  • 33、商品服务--品牌管理--OSS整合测试
    1、使用SDK进行上传就到达了如下地址,按照里面步骤来即可https://help.aliyun.com/document_detail/32008.htm?spm=a2c4g.11186623.0.0.6e0340c2gZddUH#conc......
  • PostGIS之几何有效性
    1.概述PostGIS是PostgreSQL数据库一个空间数据库扩展,它添加了对地理对象的支持,允许在SQL中运行空间查询PostGIS官网:AboutPostGIS|PostGISPostGIS官方教程:PostGIS......
  • 【Unity TIL】6. 如何判断两条线段是否相交
    AABB碰撞检测,也就是轴对齐碰撞检测,用平行于x,y轴的矩形表示物体。如何判断两个矩形是否相撞,可以通过分别判断x,y轴上的线段是否相交。假设线段分别为(s1,e1),(s2,e2),判......
  • Vue组件之间的通信方式都有哪些?
    一、组件间通信的概念我们通常把组件间通信这个词进行拆分组件通信都知道组件是vue最强大的功能之一,vue中每一个.vue我们都可以视之为一个组件通信指的是发......
  • 11.动态SQL
    什么是动态SQL:动态SQL就是指根据不同的条件生成不同的SQL语句if:这条语句提供了可选的查找文本功能。//动态sql--ifList<Blog>getBlog(Map<String,Object>map);<s......
  • 2023.1.7(Atcoder Beginner Contest 284)
    A.HappyNewYear2023Linkhttps://atcoder.jp/contests/abc284/tasks/abc284_dStatement将给定的\(N\)分解成\(N=p^2\cdotq\)的形式,其中\(p,q\)为两个不......
  • MIPS32函数调用实例汇编分析
    我们首先来看函数的调用过程,下面的代码属于调用者。la$t9,cgibin_parse_requestlui$a0,0x41#'A'lui$a2,2li$a0,sub_409A6Cjalr$t9......
  • Linux运维笔记[9]-磁盘管理
    RAID简介[https://zhuanlan.zhihu.com/p/356299159][https://www.cnblogs.com/qi-yuan/p/11735525.html]磁盘阵列(RedundantArraysofIndependentDisks,RAID),有“独立磁......
  • SpringBoot笔记--文件配置加载顺序+整合其他框架
    内部文件配置加载顺序外部文件配置加载顺序jar包配置整合Junit若是业务管理类和测试类在同一个包下面,那么这句话,可以不加括号,只写注解名称否则,就必须指定到包......
  • 封装
    1.我们程序设计追求“高内聚,低耦合”。高内聚就是类的内部数据操作细节自己完成,不允许外部干涉;低耦合:仅暴露少量的方法给外部使用。2.封装(数据的隐藏)通常,应禁止直接......