实例介绍
【实例简介】Optimizing software in C
An optimization guide for Windows, Linux, and Mac
platforms
By Agner Fog. Technical University of Denmark.
Copyright © 2004 - 2019. Last updated 2019-10-18.
【实例截图】
【核心代码】
Contents 1 Introduction ....................................................................................................................... 3 1.1 The costs of optimizing ............................................................................................... 4 2 Choosing the optimal platform........................................................................................... 5 2.1 Choice of hardware platform ....................................................................................... 5 2.2 Choice of microprocessor ........................................................................................... 6 2.3 Choice of operating system......................................................................................... 6 2.4 Choice of programming language ............................................................................... 7 2.5 Choice of compiler...................................................................................................... 9 2.6 Choice of function libraries........................................................................................ 11 2.7 Choice of user interface framework........................................................................... 13 2.8 Overcoming the drawbacks of the C language...................................................... 14 3 Finding the biggest time consumers ................................................................................ 15 3.1 How much is a clock cycle? ...................................................................................... 15 3.2 Use a profiler to find hot spots .................................................................................. 16 3.3 Program installation .................................................................................................. 18 3.4 Automatic updates .................................................................................................... 18 3.5 Program loading ....................................................................................................... 18 3.6 Dynamic linking and position-independent code ....................................................... 19 3.7 File access................................................................................................................ 19 3.8 System database ...................................................................................................... 19 3.9 Other databases ....................................................................................................... 20 3.10 Graphics ................................................................................................................. 20 3.11 Other system resources.......................................................................................... 20 3.12 Network access ...................................................................................................... 20 3.13 Memory access....................................................................................................... 21 3.14 Context switches..................................................................................................... 21 3.15 Dependency chains ................................................................................................ 21 3.16 Execution unit throughput ....................................................................................... 21 4 Performance and usability ............................................................................................... 22 5 Choosing the optimal algorithm ....................................................................................... 23 6 Development process...................................................................................................... 24 7 The efficiency of different C constructs........................................................................ 25 7.1 Different kinds of variable storage............................................................................. 25 7.2 Integers variables and operators............................................................................... 28 7.3 Floating point variables and operators ...................................................................... 31 7.4 Enums ...................................................................................................................... 33 7.5 Booleans................................................................................................................... 33 7.6 Pointers and references............................................................................................ 35 7.7 Function pointers ...................................................................................................... 37 7.8 Member pointers....................................................................................................... 37 7.9 Smart pointers .......................................................................................................... 37 7.10 Arrays ..................................................................................................................... 38 7.11 Type conversions.................................................................................................... 40 7.12 Branches and switch statements............................................................................. 43 7.13 Loops...................................................................................................................... 44 2 7.14 Functions ................................................................................................................ 47 7.15 Function parameters ............................................................................................... 49 7.16 Function return types .............................................................................................. 50 7.17 Function tail calls .................................................................................................... 51 7.18 Recursive functions................................................................................................. 51 7.19 Structures and classes............................................................................................ 52 7.20 Class data members (instance variables) ............................................................... 53 7.21 Class member functions (methods)......................................................................... 54 7.22 Virtual member functions ........................................................................................ 55 7.23 Runtime type identification (RTTI)........................................................................... 55 7.24 Inheritance.............................................................................................................. 55 7.25 Constructors and destructors .................................................................................. 56 7.26 Unions .................................................................................................................... 56 7.27 Bitfields ................................................................................................................... 57 7.28 Overloaded functions .............................................................................................. 57 7.29 Overloaded operators ............................................................................................. 58 7.30 Templates............................................................................................................... 58 7.31 Threads .................................................................................................................. 61 7.32 Exceptions and error handling ................................................................................ 62 7.33 Other cases of stack unwinding .............................................................................. 66 7.34 Propagation of NAN and INF .................................................................................. 66 7.35 Preprocessing directives......................................................................................... 67 7.36 Namespaces........................................................................................................... 67 8 Optimizations in the compiler .......................................................................................... 67 8.1 How compilers optimize ............................................................................................ 67 8.2 Comparison of different compilers............................................................................. 75 8.3 Obstacles to optimization by compiler....................................................................... 79 8.4 Obstacles to optimization by CPU............................................................................. 83 8.5 Compiler optimization options ................................................................................... 83 8.6 Optimization directives.............................................................................................. 84 8.7 Checking what the compiler does ............................................................................. 86 9 Optimizing memory access ............................................................................................. 89 9.1 Caching of code and data ......................................................................................... 89 9.2 Cache organization................................................................................................... 89 9.3 Functions that are used together should be stored together...................................... 90 9.4 Variables that are used together should be stored together ...................................... 90 9.5 Alignment of data...................................................................................................... 92 9.6 Dynamic memory allocation...................................................................................... 92 9.7 Data structures and container classes ...................................................................... 95 9.8 Strings .................................................................................................................... 102 9.9 Access data sequentially ........................................................................................ 102 9.10 Cache contentions in large data structures ........................................................... 103 9.11 Explicit cache control ............................................................................................ 105 10 Multithreading.............................................................................................................. 107 10.1 Simultaneous multithreading................................................................................. 109 11 Out of order execution................................................................................................. 110 12 Using vector operations............................................................................................... 112 12.1 AVX instruction set and YMM registers ................................................................. 114 12.2 AVX512 instruction set and ZMM registers ........................................................... 114 12.3 Automatic vectorization......................................................................................... 115 12.4 Using intrinsic functions ........................................................................................ 118 12.5 Using vector classes ............................................................................................. 121 12.6 Transforming serial code for vectorization............................................................. 126 12.7 Mathematical functions for vectors........................................................................ 128 12.8 Aligning dynamically allocated memory................................................................. 129 12.9 Aligning RGB video or 3-dimensional vectors ....................................................... 129 12.10 Conclusion.......................................................................................................... 129 13 Making critical code in multiple versions for different instruction sets........................... 131 3 13.1 CPU dispatch strategies........................................................................................ 132 13.2 Model-specific dispatching.................................................................................... 133 13.3 Difficult cases........................................................................................................ 134 13.4 Test and maintenance .......................................................................................... 135 13.5 Implementation ..................................................................................................... 136 13.6 CPU dispatching at load time in Linux................................................................... 138 13.7 CPU dispatching in Intel compiler ......................................................................... 139 14 Specific optimization topics ......................................................................................... 141 14.1 Use lookup tables ................................................................................................. 141 14.2 Bounds checking .................................................................................................. 144 14.3 Use bitwise operators for checking multiple values at once................................... 145 14.4 Integer multiplication............................................................................................. 146 14.5 Integer division...................................................................................................... 147 14.6 Floating point division ........................................................................................... 149 14.7 Do not mix float and double .................................................................................. 150 14.8 Conversions between floating point numbers and integers ................................... 150 14.9 Using integer operations for manipulating floating point variables ......................... 151 14.10 Mathematical functions ....................................................................................... 155 14.11 Static versus dynamic libraries............................................................................ 155 14.12 Position-independent code.................................................................................. 157 14.13 System programming.......................................................................................... 159 15 Metaprogramming ....................................................................................................... 160 15.1 Template metaprogramming ................................................................................. 160 15.2 Metaprogramming with constexpr branches.......................................................... 163 15.3 Metaprogramming with constexpr functions .......................................................... 164 16 Testing speed.............................................................................................................. 164 16.1 Using performance monitor counters .................................................................... 166 16.2 The pitfalls of unit-testing ...................................................................................... 167 16.3 Worst-case testing ................................................................................................ 168 17 Optimization in embedded systems............................................................................. 169 18 Overview of compiler options....................................................................................... 171 19 Literature..................................................................................................................... 175 20 Copyright notice .......................................................................................................... 176
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论