Considerations about C++ performance and efficiency

1. Three perspectives on "efficiency"

1. Encapsulation is supported, and the same interface can be implemented to experiment with the advantages and disadvantages of different algorithms and data structure efficiency.
2. The impact of the construction and destruction of too many objects on performance
3. The choice of library

2. Basic ideas and methods

1. The 80-20 rule
80% of a program's resources are spent on 20% of the code.
Explain that the overall performance of the software is determined by a small part of the code.
The performance bottleneck of the program cannot be determined by guessing or intuitively, otherwise a lot of energy will be wasted, because the performance characteristics of the program tend to be highly non-intuitive.
Need to observe or experiment to determine which is the 20% part of the code, this has to resort to program analyzers, such as gprof, valgrind, etc.
Don't worry too much about how many times the detailed code is executed, it depends on the response time of the program, because customers hate the program that makes them wait.
The program analyzer is limited by the data it uses. Don't rely too much on a certain set of test data during testing. Use test data with multiple characteristics as much as possible to analyze code performance.

2. Use delaying tactics.
In some cases, the calculation can be delayed, namely lazy evaluation. For example, with copy-on-write technology, data is shared when reading, and a copy is only copied for writing when writing.
use:

  • Avoid object duplication
  • Can distinguish the read and write operations of the object
  • Avoid unnecessary database read operations
  • Avoid unnecessary numerical calculation operations

Use scenario: It is not used in occasions where calculations are necessary, but it is suitable for occasions where the program is required to perform certain calculations and these calculations can be avoided.

3. Do things before they are required to be done. The
idea behind: If the expected program often uses a certain calculation, you can use the new data structure to reduce the average cost of each calculation. For example, cache the intermediate results of the last calculation; cache the database data that has been accessed, and directly use the data that has been accessed the next time.

4. Minimize the generation of temporary objects. The
so-called C++ temporary objects are unnamed objects that are not visible in the code and are generated by the compiler as needed.
Timing:

  • Implicit type conversion caused by type mismatch

  • Coping strategies when the function returns an object :
  • For temporary objects, keep the type consistent when assigning values ​​or passing parameters, and avoid passing values ​​when passing composite types when passing parameters, and try to replace them with references.
  • The return value, using "return value optimization (RVO)", is the optimization mechanism of the compiler.

5. Use other
libraries. It is necessary to select libraries for performance bottlenecks. At the same time, it is necessary to examine from multiple dimensions such as efficiency, scalability, portability, and type safety.

6, C++ object-oriented features support cost It is
necessary to understand the cost of virtual functions, multiple inheritance, virtual base classes, and runtime type recognition.
If skills with these characteristics are needed, we have to bear these costs.

Guess you like

Origin blog.csdn.net/xunye_dream/article/details/114951091