Jump to content
Nytro

Optimizing software in C++

Recommended Posts

Optimizing software in C++

An optimization guide for Windows, Linux and Mac platforms

Contents
1 Introduction ....................................................................................................................... 3
1.1 The costs of optimizing ............................................................................................... 4
2 Choosing the optimal platform........................................................................................... 4
2.1 Choice of hardware platform....................................................................................... 4
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 ....................................................................................................... 19
3.6 Dynamic linking and position-independent code ....................................................... 19
3.7 File access................................................................................................................21
3.8 System database ...................................................................................................... 21
3.9 Other databases ....................................................................................................... 21
3.10 Graphics and other system resources..................................................................... 22
3.11 Network access ...................................................................................................... 22
3.12 Memory access....................................................................................................... 22
3.13 Context switches..................................................................................................... 22
3.14 Dependency chains ................................................................................................ 23
3.15 Execution unit throughput ....................................................................................... 23
4 Performance and usability ............................................................................................... 23
5 Choosing the optimal algorithm....................................................................................... 25
6 Development process...................................................................................................... 26
7 The efficiency of different C++ constructs........................................................................ 26
7.1 Different kinds of variable storage............................................................................. 26
7.2 Integers variables and operators............................................................................... 29
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 .......................................................................................................... 38
7.10 Arrays ..................................................................................................................... 38
7.11 Type conversions.................................................................................................... 40
7.12 Branches and switch statements............................................................................. 43
7.13 Loops...................................................................................................................... 45
7.14 Functions ................................................................................................................ 47
2
7.15 Function parameters ............................................................................................... 49
7.16 Function return types .............................................................................................. 50
7.17 Structures and classes............................................................................................ 50
7.18 Class data members (properties) ............................................................................ 51
7.19 Class member functions (methods)......................................................................... 52
7.20 Virtual member functions ........................................................................................ 53
7.21 Runtime type identification (RTTI)........................................................................... 53
7.22 Inheritance.............................................................................................................. 53
7.23 Constructors and destructors .................................................................................. 54
7.24 Unions ....................................................................................................................54
7.25 Bitfields................................................................................................................... 55
7.26 Overloaded functions .............................................................................................. 55
7.27 Overloaded operators ............................................................................................. 55
7.28 Templates............................................................................................................... 56
7.29 Threads .................................................................................................................. 59
7.30 Exceptions and error handling ................................................................................ 60
7.31 Other cases of stack unwinding .............................................................................. 63
7.32 Preprocessing directives......................................................................................... 64
7.33 Namespaces........................................................................................................... 64
8 Optimizations in the compiler .......................................................................................... 64
8.1 How compilers optimize ............................................................................................ 64
8.2 Comparison of different compilers............................................................................. 72
8.3 Obstacles to optimization by compiler....................................................................... 76
8.4 Obstacles to optimization by CPU............................................................................. 79
8.5 Compiler optimization options ................................................................................... 79
8.6 Optimization directives.............................................................................................. 81
8.7 Checking what the compiler does ............................................................................. 82
9 Optimizing memory access ............................................................................................. 85
9.1 Caching of code and data ......................................................................................... 85
9.2 Cache organization................................................................................................... 85
9.3 Functions that are used together should be stored together...................................... 87
9.4 Variables that are used together should be stored together ...................................... 87
9.5 Alignment of data...................................................................................................... 88
9.6 Dynamic memory allocation...................................................................................... 89
9.7 Container classes ..................................................................................................... 91
9.8 Strings ...................................................................................................................... 94
9.9 Access data sequentially .......................................................................................... 94
9.10 Cache contentions in large data structures ............................................................. 95
9.11 Explicit cache control .............................................................................................. 97
10 Multithreading................................................................................................................ 99
10.1 Hyperthreading ..................................................................................................... 101
11 Out of order execution................................................................................................. 101
12 Using vector operations............................................................................................... 103
12.1 AVX instruction set and YMM registers................................................................. 104
12.2 Automatic vectorization......................................................................................... 105
12.3 Explicit vectorization ............................................................................................. 107
12.4 Mathematical functions for vectors........................................................................ 120
12.5 Aligning dynamically allocated memory................................................................. 122
12.6 Aligning RGB video or 3-dimensional vectors ....................................................... 122
12.7 Conclusion............................................................................................................ 123
13 Make critical code in multiple versions for different CPUs............................................ 123
13.1 CPU dispatch strategies........................................................................................ 124
13.2 Test and maintenance .......................................................................................... 126
13.3 Implementation ..................................................................................................... 126
13.4 CPU dispatching in Gnu compiler ......................................................................... 128
13.5 CPU dispatching in Intel compiler ......................................................................... 129
14 Specific optimization tips ............................................................................................. 135
14.1 Use lookup tables ................................................................................................. 135
3
14.2 Bounds checking .................................................................................................. 137
14.3 Use bitwise operators for checking multiple values at once................................... 138
14.4 Integer multiplication............................................................................................. 139
14.5 Integer division...................................................................................................... 140
14.6 Floating point division ........................................................................................... 142
14.7 Don’t mix float and double..................................................................................... 143
14.8 Conversions between floating point numbers and integers ................................... 144
14.9 Using integer operations for manipulating floating point variables......................... 145
14.10 Mathematical functions ....................................................................................... 148
15 Metaprogramming ....................................................................................................... 149
16 Testing speed.............................................................................................................. 152
16.1 The pitfalls of unit-testing ...................................................................................... 154
16.2 Worst-case testing ................................................................................................ 154
17 Overview of compiler options....................................................................................... 156
18 Literature..................................................................................................................... 159
19 Copyright notice .......................................................................................................... 160

Download:

http://www.speedyshare.com/files/24051378/optimizing_cpp.pdf

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...