C++ Introduction to performance


Example

C and C++ are well known as high-performance languages - largely due to the heavy amount of code customization, allowing a user to specify performance by choice of structure.

When optimizing it is important to benchmark relevant code and completely understand how the code will be used.

Common optimization mistakes include:

  • Premature optimization: Complex code may perform worse after optimization, wasting time and effort. First priority should be to write correct and maintainable code, rather than optimized code.
  • Optimization for the wrong use case: Adding overhead for the 1% might not be worth the slowdown for the other 99%
  • Micro-optimization: Compilers do this very efficiently and micro-optimization can even hurt the compilers ability to further optimize the code

Typical optimization goals are:

  • To do less work
  • To use more efficient algorithms/structures
  • To make better use of hardware

Optimized code can have negative side effects, including:

  • Higher memory usage
  • Complex code -being difficult to read or maintain
  • Compromised API and code design