Download Optimizing software in C++
Transcript
6.18 Runtime type identification (RTTI)........................................................................... 50 6.19 Inheritance.............................................................................................................. 50 6.20 Constructors and destructors .................................................................................. 51 6.21 Unions .................................................................................................................... 51 6.22 Bitfields ................................................................................................................... 51 6.23 Overloaded functions .............................................................................................. 52 6.24 Overloaded operators ............................................................................................. 52 6.25 Templates............................................................................................................... 52 6.26 Threads .................................................................................................................. 55 6.27 Exception handling.................................................................................................. 56 6.28 Other cases of stack unwinding .............................................................................. 59 6.29 Preprocessing directives ......................................................................................... 59 6.30 Namespaces........................................................................................................... 59 7 Optimizations in the compiler .......................................................................................... 60 7.1 How compilers optimize ............................................................................................ 60 7.2 Comparison of different compilers............................................................................. 68 7.3 Obstacles to optimization by compiler ....................................................................... 71 7.4 Obstacles to optimization by CPU............................................................................. 74 7.5 Compiler optimization options ................................................................................... 75 7.6 Optimization directives.............................................................................................. 76 7.7 Checking what the compiler does ............................................................................. 78 8 Optimizing memory access ............................................................................................. 81 8.1 Caching of code and data ......................................................................................... 81 8.2 Cache organization ................................................................................................... 81 8.3 Functions that are used together should be stored together...................................... 82 8.4 Variables that are used together should be stored together ...................................... 82 8.5 Alignment of data...................................................................................................... 84 8.6 Dynamic memory allocation ...................................................................................... 84 8.7 Container classes ..................................................................................................... 87 8.8 Strings ...................................................................................................................... 89 8.9 Access data sequentially .......................................................................................... 89 8.10 Cache contentions in large data structures ............................................................. 90 8.11 Explicit cache control .............................................................................................. 92 9 Using multiple CPU cores................................................................................................ 94 10 Out of order execution ................................................................................................... 96 11 Using vector operations................................................................................................. 98 11.1 Automatic vectorization........................................................................................... 99 11.2 Explicit vectorization ............................................................................................. 101 11.3 Mathematical functions for vectors........................................................................ 114 11.4 Aligning dynamically allocated memory................................................................. 116 11.5 Aligning RGB video or 3-dimensional vectors ....................................................... 116 11.6 Conclusion............................................................................................................ 117 12 Make critical code in multiple versions for different CPU's ........................................... 117 12.1 CPU dispatching in Intel compiler ......................................................................... 120 13 Specific optimization tips ............................................................................................. 123 13.1 Use lookup tables ................................................................................................. 123 13.2 Bounds checking .................................................................................................. 125 13.3 Use bitwise operators for checking multiple values at once................................... 126 13.4 Integer multiplication ............................................................................................. 127 13.5 Integer division...................................................................................................... 128 13.6 Floating point division ........................................................................................... 130 13.7 Don't mix float and double..................................................................................... 131 13.8 Conversions between floating point numbers and integers ................................... 132 13.9 Using integer operations for manipulating floating point variables ......................... 133 13.10 Mathematical functions ....................................................................................... 136 14 Testing speed.............................................................................................................. 136 14.1 The pitfalls of unit-testing ...................................................................................... 138 15 Overview of compiler options....................................................................................... 140 2