telecom • networking • design

an blog

Optimizing C and C++ code

Leave a comment

Several techniques for optimizing C and C++ code are covered:

  • 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

Optimizing C and C++ Code

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s