Jump to content
em

Optimizing C and C++ Code

Recommended Posts

Posted

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++ Code


Adjust structure sizes to power of two
Place case labels in narrow range
Place frequent case labels first
Break big switch statements into nested switches
Minimize local variables
Declare local variables in the inner most scope
Reduce the number of parameters
Use references for parameter passing and return value for types bigger than 4 bytes
Don't define a return value if not used
Consider locality of reference for code and data
Prefer int over char and short
Define lightweight constructors
Prefer initialization over assignment
Use constructor initialization lists
Do not declare "just in case" virtual functions
In-line 1 to 3 line functions

C++ Optimization Strategies and Techniques


General Strategies

There'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 Bomb
Optimization Strategies that Work
Example of Selecting the Proper Algorithm
C++ Design Considerations

When 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 containers
Consider using references instead of pointers
Consider two-phase construction
Limit exception handling
Avoid Runtime Type Identification
Prefer stdio to iostream
Evaluate alternative libraries
C++ 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 reference
Postpone variable declaration as long as possible
Prefer initialization over assignment
Use constructor initialization lists
Prefer operator= over operator alone
Use prefix operators
Use explicit constructors
Final Optimizations

Your 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 functions
Avoid temporary objects: the return value optimization
Be aware of the cost of virtual functions
Return objects via reference parameters
Consider per-class allocation
Consider STL container allocators
The "empty member" optimization
Template metaprogramming
Copy on write
Compiler Optimizations

A 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 settings
C++ language settings
The "ultimate" compiler settings
Use the novtable option for abstract classes (Microsoft Visual C++)
Indicate functions that don't throw exceptions
Use the fastcall calling convention (Microsoft Visual C++)
Warning: Unsafe optimizations
Appendices

STL Container efficiency table
Relative costs of common programming operations
C code tuning and C++ efficiency resources
The Author

  • Upvote 1

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...