http://www.agner.org/optimize/#manuals

阅读笔记Optimizing software in C++  

7. The efficiency of different C++ constructs

栈的速度快是因为,总是反复访问同一段地址,如果没有大的数组,肯定实在L1 cahce中.

全局静态区,global,static变量,float constants, string constants, array initializer lists,switch statement jump tables, and virtual function tables.

unsigned 和signed:除以常数,unsigned快,%同样。转换到float,signed快些。

指针加整数偏移其实是加上整数乘以对象的size:

// Example

struct abc {int a; int b; int c;};
abc * p; int i;
p = p + i;     ->p += i*sizeof(abc);

所以对象的size最好是2^n。而两个指针减时,还需要除法效率更低,除非对象size是2^n.

x = *(p++) is more efficient than x = *(++p),后者需要等待p自增后才能读取x.

Conversion of a signed integer to a float or double takes 4 - 16 clock cycles。Conversion of an unsigned integer takes longer time. It is faster to first convert the unsigned integer to a signed integer if there is no risk of overflow:
// Example
unsigned int u; double d;
d = (double)(signed int)u; // Faster, but risk of overflow

Conversion of a floating point number to an integer takes a very long time unless the SSE2 or later instruction set is enabled. Typically, the conversion takes 50 - 100 clock cycles. The reason is that the C/C++ standard specifies truncation so the floating point rounding mode has to be changed to truncation and back again. 所以避免浮点到整型的转换,必要时:循环内使用浮点中间结果,算完再只进行一次转换;使用rounding函数转换而不是直接截断。

分支预测,branch misprediction代价很大,需要12-25个cycles恢复,而预测正常只需要0-2个cycles.

switch case语句,case 的value越接近(递增)效率越高,因为可以处理为跳转表。values越分散效率越低,因为要处理为branch tree。

函数跳转返回takes 4+ cycles.

 8 Optimizations in the compiler

理解编译器能做的优化

9 Optimizing memory access

1.cache 结构

例如,8kb的cache,line size = 64,8*1024/64 = 128,128条lines 组织成32 sets x 4 ways .  任一内存地址只能放到某一个set,计算方法为:

(set) = (memory address) / (line size) % (number of sets). 地址10000的内容会放到28组中的一条cache line 中,(set) = (10000 / 64) % 32 = 28.

当读取的两个地址映射到同一组set时,可能就会产生cache miss.

4. Variables that are used together should be stored together

12 Using vector operations 

讲述simd指令,使用支持automatic vectorization的编译器,使用vector class library (VCL) make code readable

13.CPU dispatching

making the most critical parts of the code in multiple versions for different CPUs.

应根据cpu支持的指令集来适配,而不是brand, family and model number

14 Specific optimization topics

1.lookup tables

静态变量可能分散在不同的内存地址,不利于cache(破坏了局部访问).可以查找表从静态区拷贝到栈上.

如下,FactorialTableA存在静态区,而FactorialTable会在调用函数CriticalInnerFunction时,把数据从静态区拷贝到栈上。

// Example 14.1c

const int FactorialTableA[13] = {1, 1, 2, 6, 24, 120, 720,
5040, 40320, 362880, 3628800, 39916800, 479001600};
void CriticalInnerFunction () {
// Table of factorials:
const int FactorialTable[13] = {1, 1, 2, 6, 24, 120, 720,
5040, 40320, 362880, 3628800, 39916800, 479001600};

...

}

2 Bounds checking

// Example 14.4a
const int size = 16; int i;
float list[size];
...
if (i < 0 || i >= size) {
cout << "Error: Index out of range";
}
else {}

替换为只有一次比较:

// Example 14.4b
if ((unsigned int)i >= (unsigned int)size) {
cout << "Error: Index out of range";
}
else {}

// Example 14.5a
const int min = 100, max = 110; int i;
...
if (i >= min && i <= max) { ...
can be changed to:
// Example 14.5b
if ((unsigned int)(i - min) <= (unsigned int)(max - min)) { ..

3 Use bitwise operators for checking multiple values at once

使用位运算简化条件判断,比较操作.

// Example 14.7a. Testing multiple conditions
enum Weekdays {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
};
Weekdays Day;
if (Day == Tuesday || Day == Wednesday || Day == Friday) {
DoThisThreeTimesAWeek();
}

会有三次比较,可以使用位运算替代:

// Example 14.7b. Testing multiple conditions using &
enum Weekdays {
Sunday = 1, Monday = 2, Tuesday = 4, Wednesday = 8,
Thursday = 0x10, Friday = 0x20, Saturday = 0x40
};
Weekdays Day;
if (Day & (Tuesday | Wednesday | Friday)) {
DoThisThreeTimesAWeek();
}

Boolean operators &&, ||, ! 与 bitwise operators &, |, ~ 的区别:前者结果只有两种true (1) or false (0),而位运算更快因为不产生分支。

4.整型乘法(3 - 10 clock cycles),编译器会用右移和加代替乘法

5.整型除法(27 - 80 clock cycles)

除以常数更快是因为编译器会优化a / b 为a * (2^n / b) >> n.   %与/规律相同。

// Example 14.10
int a, b, c;
a = b / c; // This is slow
a = b / 10; // Division by a constant is faster
a = (unsigned int)b / 10; // Still faster if unsigned
a = b / 16; // Faster if divisor is a power of 2
a = (unsigned int)b / 16; // Still faster if unsigned

6.浮点除法(20 - 45 clock cycles),使用乘以倒数替代

7.Don't mix float and double

float与double运算同样快。但是互相转换很耗时。C/C++ 默认浮点常量是double精度的。

8.浮点转整型

C++默认浮点到整型转换都是向0截断(truncation)。而truncation(40 clock cycles)耗时是rounding的3倍,除非使用sse2指令集.

9.整型转浮点

比浮点转整型快(5-20 clock cycles),有符号比无符号的整型快。所以不溢出时先转换成有符号再转换成浮点更快:

// Example 14.22b
unsigned int u; double d;
d = (double)(signed int)u;

[阅读笔记]Software optimization resources的更多相关文章

  1. Mysql DOC阅读笔记

    Mysql DOC阅读笔记 转自我的Github Speed of SELECT Statements 合理利用索引 隔离调试查询中花费高的部分,例如函数调用是在结果集中的行执行还是全表中的行执行 最 ...

  2. Mybatis3.3——源码阅读笔记

    目录 Mybatis--Source阅读笔记 兵马未动,日志先行 异常 缓存 回收机制适配器 回收机制优化缓存 事务缓存 调试型缓存--日志缓存 解析 类型处理器 IO VFS Resource Re ...

  3. Deep Learning of Graph Matching 阅读笔记

    Deep Learning of Graph Matching 阅读笔记 CVPR2018的一篇文章,主要提出了一种利用深度神经网络实现端到端图匹配(Graph Matching)的方法. 该篇文章理 ...

  4. 《STL源代码剖析》---stl_deque.h阅读笔记(2)

    看完,<STL源代码剖析>---stl_deque.h阅读笔记(1)后.再看代码: G++ 2.91.57,cygnus\cygwin-b20\include\g++\stl_deque. ...

  5. 阅读笔记——《How a Facebook rejection pushed me to start and grow a profitable business in 12 months》

    阅读笔记——<How a Facebook rejection pushed me to start and grow a profitable business in 12 months> ...

  6. 阅读笔记 1 火球 UML大战需求分析

    伴随着七天国庆的结束,紧张的学习生活也开始了,首先声明,阅读笔记随着我不断地阅读进度会慢慢更新,而不是一次性的写完,所以会重复的编辑.对于我选的这本   <火球 UML大战需求分析>,首先 ...

  7. 《uml大战需求分析》阅读笔记05

    <uml大战需求分析>阅读笔记05 这次我主要阅读了这本书的第九十章,通过看这章的知识了解了不少的知识开发某系统的重要前提是:这个系统有谁在用?这些人通过这个系统能做什么事? 一般搞清楚这 ...

  8. <<UML大战需求分析>>阅读笔记(2)

    <<UML大战需求分析>>阅读笔记(2)> 此次读了uml大战需求分析的第三四章,我发现这本书讲的特别的好,由于这学期正在学习设计模式这本书,这本书就讲究对uml图的利用 ...

  9. uml大战需求分析阅读笔记01

    <<UML大战需求分析>>阅读笔记(1) 刚读了uml大战需求分析的第一二章,读了这些内容之后,令我深有感触.以前学习uml这门课的时候,并没有好好学,那时我认为这门课并没有什 ...

随机推荐

  1. WebAPI使用多个xml文件生成帮助文档

    一.前言 上篇有提到在WebAPI项目内,通过在Nuget里安装(Microsoft.AspNet.WebApi.HelpPage)可以根据注释生成帮助文档,查看代码实现会发现是基于解析项目生成的xm ...

  2. 非常强大的table根据表头排序,点击表头名称,对其内容排序

    js代码: /** * 通过表头对表列进行排序 * * @param sTableID * 要处理的表ID<table id=''> * @param iCol * 字段列id eg: 0 ...

  3. Android学习笔记 - BitmapFun解析

    如果图片资源是静态的,当我们要在View上显示图片时,只需要简单的将图片赋值给ImageView就可以了,但如果需要浏览网络上的图片时该如何做呢?有可能图片很大,有可能网速很慢并且不稳定,这种情况下该 ...

  4. Android 控件属性介绍

    1.LinearLayout(线性布局): 可以分为水平线性:android:orientation= " horizontal " 和垂直线性:android:orientati ...

  5. Lombok 安装、入门 - 消除冗长的 java 代码

    lombok 提供了简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 java 代码. lombok 的官方网址:http://projectlombok.org/  lombok 安装1. ...

  6. quick-cocos2d-x :加入学习

    语法不熟悉, 接口不熟悉. 部分不理解. 超级初学者. 好好学习, 一直在学习 使用Quick-Cocos2d-x搭建一个横版过关游戏 推荐官方的文档和例子  demo

  7. 使用Python从Markdown文档中自动生成标题导航

    概述 知识与思路 代码实现 概述 Markdown 很适合于技术写作,因为技术写作并不需要花哨的排版和内容, 只要内容生动而严谨,文笔朴实而优美. 为了编写对读者更友好的文章,有必要生成文章的标题导航 ...

  8. 阿里yum源

    转:http://mirrors.aliyun.com/help/centos?spm=5176.bbsr150321.0.0.d6ykiD 1.备份 mv /etc/yum.repos.d/Cent ...

  9. PHP动态图像的创建要如何实现呢?

    with-gd=[/path/to/gd] --with-jpeg-dir=[/path/to/jpeg-6b] --with-t1lib=[/path/to/t1lib]  完成添加后执行make命 ...

  10. MBP使用笔记

    1. 链接测试机命令: 登录:ssh ria@000.000.000.000 然后输入密码即可 退出:exit 2. SwitchySharp导入的是bak文件. 3. 使用goagentFQ的使用的 ...