Nytro Posted August 31, 2010 Report Posted August 31, 2010 Optimizing software in C++An optimization guide for Windows, Linux and Mac platformsContents1 Introduction ....................................................................................................................... 31.1 The costs of optimizing ............................................................................................... 42 Choosing the optimal platform........................................................................................... 42.1 Choice of hardware platform....................................................................................... 42.2 Choice of microprocessor ........................................................................................... 62.3 Choice of operating system......................................................................................... 62.4 Choice of programming language ............................................................................... 72.5 Choice of compiler ...................................................................................................... 92.6 Choice of function libraries........................................................................................ 112.7 Choice of user interface framework........................................................................... 132.8 Overcoming the drawbacks of the C++ language...................................................... 143 Finding the biggest time consumers ................................................................................ 153.1 How much is a clock cycle? ...................................................................................... 153.2 Use a profiler to find hot spots .................................................................................. 163.3 Program installation .................................................................................................. 183.4 Automatic updates .................................................................................................... 183.5 Program loading ....................................................................................................... 193.6 Dynamic linking and position-independent code ....................................................... 193.7 File access................................................................................................................213.8 System database ...................................................................................................... 213.9 Other databases ....................................................................................................... 213.10 Graphics and other system resources..................................................................... 223.11 Network access ...................................................................................................... 223.12 Memory access....................................................................................................... 223.13 Context switches..................................................................................................... 223.14 Dependency chains ................................................................................................ 233.15 Execution unit throughput ....................................................................................... 234 Performance and usability ............................................................................................... 235 Choosing the optimal algorithm....................................................................................... 256 Development process...................................................................................................... 267 The efficiency of different C++ constructs........................................................................ 267.1 Different kinds of variable storage............................................................................. 267.2 Integers variables and operators............................................................................... 297.3 Floating point variables and operators ...................................................................... 317.4 Enums ......................................................................................................................337.5 Booleans................................................................................................................... 337.6 Pointers and references............................................................................................ 357.7 Function pointers ...................................................................................................... 377.8 Member pointers....................................................................................................... 377.9 Smart pointers .......................................................................................................... 387.10 Arrays ..................................................................................................................... 387.11 Type conversions.................................................................................................... 407.12 Branches and switch statements............................................................................. 437.13 Loops...................................................................................................................... 457.14 Functions ................................................................................................................ 4727.15 Function parameters ............................................................................................... 497.16 Function return types .............................................................................................. 507.17 Structures and classes............................................................................................ 507.18 Class data members (properties) ............................................................................ 517.19 Class member functions (methods)......................................................................... 527.20 Virtual member functions ........................................................................................ 537.21 Runtime type identification (RTTI)........................................................................... 537.22 Inheritance.............................................................................................................. 537.23 Constructors and destructors .................................................................................. 547.24 Unions ....................................................................................................................547.25 Bitfields................................................................................................................... 557.26 Overloaded functions .............................................................................................. 557.27 Overloaded operators ............................................................................................. 557.28 Templates............................................................................................................... 567.29 Threads .................................................................................................................. 597.30 Exceptions and error handling ................................................................................ 607.31 Other cases of stack unwinding .............................................................................. 637.32 Preprocessing directives......................................................................................... 647.33 Namespaces........................................................................................................... 648 Optimizations in the compiler .......................................................................................... 648.1 How compilers optimize ............................................................................................ 648.2 Comparison of different compilers............................................................................. 728.3 Obstacles to optimization by compiler....................................................................... 768.4 Obstacles to optimization by CPU............................................................................. 798.5 Compiler optimization options ................................................................................... 798.6 Optimization directives.............................................................................................. 818.7 Checking what the compiler does ............................................................................. 829 Optimizing memory access ............................................................................................. 859.1 Caching of code and data ......................................................................................... 859.2 Cache organization................................................................................................... 859.3 Functions that are used together should be stored together...................................... 879.4 Variables that are used together should be stored together ...................................... 879.5 Alignment of data...................................................................................................... 889.6 Dynamic memory allocation...................................................................................... 899.7 Container classes ..................................................................................................... 919.8 Strings ...................................................................................................................... 949.9 Access data sequentially .......................................................................................... 949.10 Cache contentions in large data structures ............................................................. 959.11 Explicit cache control .............................................................................................. 9710 Multithreading................................................................................................................ 9910.1 Hyperthreading ..................................................................................................... 10111 Out of order execution................................................................................................. 10112 Using vector operations............................................................................................... 10312.1 AVX instruction set and YMM registers................................................................. 10412.2 Automatic vectorization......................................................................................... 10512.3 Explicit vectorization ............................................................................................. 10712.4 Mathematical functions for vectors........................................................................ 12012.5 Aligning dynamically allocated memory................................................................. 12212.6 Aligning RGB video or 3-dimensional vectors ....................................................... 12212.7 Conclusion............................................................................................................ 12313 Make critical code in multiple versions for different CPUs............................................ 12313.1 CPU dispatch strategies........................................................................................ 12413.2 Test and maintenance .......................................................................................... 12613.3 Implementation ..................................................................................................... 12613.4 CPU dispatching in Gnu compiler ......................................................................... 12813.5 CPU dispatching in Intel compiler ......................................................................... 12914 Specific optimization tips ............................................................................................. 13514.1 Use lookup tables ................................................................................................. 135314.2 Bounds checking .................................................................................................. 13714.3 Use bitwise operators for checking multiple values at once................................... 13814.4 Integer multiplication............................................................................................. 13914.5 Integer division...................................................................................................... 14014.6 Floating point division ........................................................................................... 14214.7 Don’t mix float and double..................................................................................... 14314.8 Conversions between floating point numbers and integers ................................... 14414.9 Using integer operations for manipulating floating point variables......................... 14514.10 Mathematical functions ....................................................................................... 14815 Metaprogramming ....................................................................................................... 14916 Testing speed.............................................................................................................. 15216.1 The pitfalls of unit-testing ...................................................................................... 15416.2 Worst-case testing ................................................................................................ 15417 Overview of compiler options....................................................................................... 15618 Literature..................................................................................................................... 15919 Copyright notice .......................................................................................................... 160Download:http://www.speedyshare.com/files/24051378/optimizing_cpp.pdf Quote