em Posted November 12, 2011 Report Posted November 12, 2011 O parte din programatori nu scriu cod eficient fie ca habar nu au ca scriu prost (asta e si vina IDE-ului care compileaza direct cu -O3) fie ca stiu ca oricum compilatorul le repara prostiile.De curand am avut de facut un proiect si nu aveam voie sa folosesc flagurile -O1, -O2, -O3 la compilare. Linkurile de mai jos mi-au folosit. Optimizing C and C++ CodeAdjust structure sizes to power of twoPlace case labels in narrow rangePlace frequent case labels firstBreak big switch statements into nested switchesMinimize local variablesDeclare local variables in the inner most scopeReduce the number of parametersUse references for parameter passing and return value for types bigger than 4 bytesDon't define a return value if not usedConsider locality of reference for code and dataPrefer int over char and shortDefine lightweight constructorsPrefer initialization over assignmentUse constructor initialization listsDo not declare "just in case" virtual functionsIn-line 1 to 3 line functionsC++ Optimization Strategies and TechniquesGeneral StrategiesThere's a right way and a wrong way to do optimization. Here's some strategies common to all programming endeavors that work and don't work.Optimization Strategies that BombOptimization Strategies that WorkExample of Selecting the Proper AlgorithmC++ Design ConsiderationsWhen you start working on your next app and begin to think about coding conventions, compilers, libraries, and general C++ issues, there are many factors to consider. In this section I weigh performance issues involved with C++ design considerations.Take advantage of STL containersConsider using references instead of pointersConsider two-phase constructionLimit exception handlingAvoid Runtime Type IdentificationPrefer stdio to iostreamEvaluate alternative librariesC++ Optimizations You Can Do "As You Go"Defy the software engineering mantra of "optimization procrastination." These techniques can be added to your code today! In general, these methods not only make your code more efficient, but increase readability and maintainability, too.Pass class parameters by referencePostpone variable declaration as long as possiblePrefer initialization over assignmentUse constructor initialization listsPrefer operator= over operator aloneUse prefix operatorsUse explicit constructorsFinal OptimizationsYour app is up and running. The data structures are ideal, the algorithms sublime, the code elegant, but the program - well, it's not quite living up to its potential. Time to get drastic, and with drastic measures, there are tradeoffs to consider. These optimizations are going to make your code less modular, harder to understand, and more difficult to maintain. They may cause unexpected side effects like code bloat. Your compiler may not even be able to handle some of the more advanced template-based techniques. Proceed with caution. Arm yourself with a good profiler.Inline functionsAvoid temporary objects: the return value optimizationBe aware of the cost of virtual functionsReturn objects via reference parametersConsider per-class allocationConsider STL container allocatorsThe "empty member" optimizationTemplate metaprogrammingCopy on writeCompiler OptimizationsA good compiler can have a huge effect on code performance. Most PC compilers are good, but not great, at optimization. Be aware that sometimes the compiler won't perform optimizations even though it can. The compiler assigns a higher priority to producing consistent and correct code than optimizing performance. Be thankful for small favors.C language settingsC++ language settingsThe "ultimate" compiler settingsUse the novtable option for abstract classes (Microsoft Visual C++)Indicate functions that don't throw exceptionsUse the fastcall calling convention (Microsoft Visual C++)Warning: Unsafe optimizationsAppendicesSTL Container efficiency tableRelative costs of common programming operationsC code tuning and C++ efficiency resourcesThe Author 1 Quote