Nytro Posted January 28, 2012 Report Posted January 28, 2012 Optimizing software in C++An optimization guide for Windows, Linux and Mac platformsBy Agner Fog. Copenhagen University College of Engineering.Copyright © 2004 - 2011. Last updated 2011-06-08.Contents1 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 ............................................................................... 82.5 Choice of compiler .................................................................................................... 102.6 Choice of function libraries........................................................................................ 122.7 Choice of user interface framework........................................................................... 142.8 Overcoming the drawbacks of the C++ language...................................................... 143 Finding the biggest time consumers ................................................................................ 163.1 How much is a clock cycle? ...................................................................................... 163.2 Use a profiler to find hot spots .................................................................................. 163.3 Program installation .................................................................................................. 183.4 Automatic updates .................................................................................................... 193.5 Program loading ....................................................................................................... 193.6 Dynamic linking and position-independent code ....................................................... 193.7 File access................................................................................................................213.8 System database ...................................................................................................... 223.9 Other databases ....................................................................................................... 223.10 Graphics ................................................................................................................. 223.11 Other system resources.......................................................................................... 223.12 Network access ...................................................................................................... 223.13 Memory access....................................................................................................... 233.14 Context switches..................................................................................................... 233.15 Dependency chains ................................................................................................ 233.16 Execution unit throughput ....................................................................................... 234 Performance and usability ............................................................................................... 245 Choosing the optimal algorithm....................................................................................... 256 Development process...................................................................................................... 267 The efficiency of different C++ constructs........................................................................ 277.1 Different kinds of variable storage............................................................................. 277.2 Integers variables and operators............................................................................... 307.3 Floating point variables and operators ...................................................................... 327.4 Enums ......................................................................................................................347.5 Booleans................................................................................................................... 347.6 Pointers and references............................................................................................ 367.7 Function pointers ...................................................................................................... 387.8 Member pointers....................................................................................................... 387.9 Smart pointers .......................................................................................................... 387.10 Arrays ..................................................................................................................... 397.11 Type conversions.................................................................................................... 417.12 Branches and switch statements............................................................................. 447.13 Loops...................................................................................................................... 4627.14 Functions ................................................................................................................ 487.15 Function parameters ............................................................................................... 507.16 Function return types .............................................................................................. 517.17 Structures and classes............................................................................................ 517.18 Class data members (properties) ............................................................................ 527.19 Class member functions (methods)......................................................................... 537.20 Virtual member functions ........................................................................................ 547.21 Runtime type identification (RTTI)........................................................................... 547.22 Inheritance.............................................................................................................. 547.23 Constructors and destructors .................................................................................. 557.24 Unions ....................................................................................................................567.25 Bitfields................................................................................................................... 567.26 Overloaded functions .............................................................................................. 577.27 Overloaded operators ............................................................................................. 577.28 Templates............................................................................................................... 577.29 Threads .................................................................................................................. 607.30 Exceptions and error handling ................................................................................ 617.31 Other cases of stack unwinding .............................................................................. 657.32 Preprocessing directives......................................................................................... 657.33 Namespaces........................................................................................................... 658 Optimizations in the compiler .......................................................................................... 668.1 How compilers optimize ............................................................................................ 668.2 Comparison of different compilers............................................................................. 748.3 Obstacles to optimization by compiler....................................................................... 778.4 Obstacles to optimization by CPU............................................................................. 808.5 Compiler optimization options ................................................................................... 818.6 Optimization directives.............................................................................................. 828.7 Checking what the compiler does ............................................................................. 849 Optimizing memory access ............................................................................................. 879.1 Caching of code and data ......................................................................................... 879.2 Cache organization................................................................................................... 879.3 Functions that are used together should be stored together...................................... 889.4 Variables that are used together should be stored together ...................................... 889.5 Alignment of data...................................................................................................... 909.6 Dynamic memory allocation...................................................................................... 909.7 Container classes ..................................................................................................... 929.8 Strings ...................................................................................................................... 959.9 Access data sequentially .......................................................................................... 969.10 Cache contentions in large data structures ............................................................. 969.11 Explicit cache control .............................................................................................. 9910 Multithreading.............................................................................................................. 10110.1 Hyperthreading ..................................................................................................... 10211 Out of order execution................................................................................................. 10312 Using vector operations............................................................................................... 10512.1 AVX instruction set and YMM registers................................................................. 10512.2 Automatic vectorization......................................................................................... 10612.3 Explicit vectorization ............................................................................................. 10812.4 Mathematical functions for vectors........................................................................ 12112.5 Aligning dynamically allocated memory................................................................. 12412.6 Aligning RGB video or 3-dimensional vectors ....................................................... 12412.7 Conclusion............................................................................................................ 12413 Making critical code in multiple versions for different CPUs......................................... 12513.1 CPU dispatch strategies........................................................................................ 12513.2 Difficult cases........................................................................................................ 12713.3 Test and maintenance .......................................................................................... 12913.4 Implementation ..................................................................................................... 12913.5 CPU dispatching in Gnu compiler ......................................................................... 13113.6 CPU dispatching in Intel compiler ......................................................................... 132314 Specific optimization tips ............................................................................................. 13814.1 Use lookup tables ................................................................................................. 13814.2 Bounds checking .................................................................................................. 14014.3 Use bitwise operators for checking multiple values at once................................... 14114.4 Integer multiplication............................................................................................. 14214.5 Integer division...................................................................................................... 14314.6 Floating point division ........................................................................................... 14514.7 Don’t mix float and double..................................................................................... 14614.8 Conversions between floating point numbers and integers ................................... 14614.9 Using integer operations for manipulating floating point variables......................... 14814.10 Mathematical functions ....................................................................................... 15115 Metaprogramming ....................................................................................................... 15216 Testing speed.............................................................................................................. 15516.1 The pitfalls of unit-testing ...................................................................................... 15716.2 Worst-case testing ................................................................................................ 15717 Optimization in embedded systems............................................................................. 15918 Overview of compiler options....................................................................................... 16119 Literature..................................................................................................................... 16420 Copyright notice .......................................................................................................... 165Download:http://www.agner.org/optimize/optimizing_cpp.pdf Quote
alien Posted January 28, 2012 Report Posted January 28, 2012 Prinde bine. Chiar aveam nevoie la munca. Mersi Quote