In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the optimization of C++". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Now please follow the editor's train of thought slowly and deeply. Let's study and learn what are the optimizations of C++!
1. Performance indicators depend on measurements, not guesses:
I have been working on SDK development for a period of time. Whenever we publish a version of SDK, we have to accompany the SDK usage document and performance test report. There are very detailed metric test data in the performance test report. Of course, this data cannot be guessed by ourselves, but must be tested.
two。 Pareto rule
Pareto rule, also known as the 28 principle, 80% of the execution time is spent on about 20% code, 80% of the memory is used by about 20% of the code, and 80% of the maintenance cost is spent on 20% of the code. The same is true when we do program optimization, and in most cases we only need to find the 20% code and deeply optimize it to basically meet the performance requirements.
3. Amdahl's law
SPRING
Amdahl's Law: there is a formula in which St represents the percentage of overall performance improvement after optimization, P represents the proportion of running time of the optimized part to the total running time, and Sp represents the proportion of performance improvement of the optimized part.
For example:
1. The running time of the program is 100s, of which 90s call the function func,func to improve the performance by 30%, that is, the overall efficiency of the program is 1.26214, and the overall performance of the program is improved by 26% after calling the function func,func.
two。 The running time of the program is 100s, of which 10s call the function func,func to improve the performance by 80%, that is, the overall efficiency of the program is 1.04651, and the overall performance of the program is improved by 4% after calling the function func,func.
In fact, this law also indirectly verifies the correctness of the Pareto rule, to optimize those vital parts.
After talking about the principles of performance optimization, let's talk about my optimization series planning. at present, the optimization is generally divided into the following topics:
1. Measurement analysis topics: for example, what functions are used to measure function time, and what code analysis tools are used to analyze code dynamically and statically. For example:
two。 Special topic on coding norms: analyze the efficiency of various operations of C++, including the storage efficiency of different types of variables, the efficiency of using smart pointers, loops, function parameters, virtual functions, arrays, etc., and how to make better use of them for code optimization. For example, the clock cycle occupied by each operation:
3. Compilation optimization project: various compiler performance analysis, understanding common compilation optimization options, analyzing how the compiler optimizes the code, what optimization has been done, and the obstacles to compiler optimization (what it cannot do). How to make full use of compiler optimization options. For example, optimization compiler optimization level introduction:
O0 (default option): do not enable optimization to facilitate function debugging
Og: an optimization option for debugging (more conservative than O1)
O1: conservative optimization options, with more than 40 optimization options open
Os: an optimization option that produces a smaller code size (more conservative than O2)
O2: a commonly used release optimization option that opens more than 40 optimization options on the basis of O1, including rules such as automatic inlining
O3: a more radical optimization option (with the lowest tolerance for error coding), which opens more than a dozen additional optimization options on top of O2
Ofast: turn on performance tuning options that can cause non-compliance with standards such as IEEE floating-point numbers.
As shown in the figure:
4. Memory optimization project: memory usage is often a key factor affecting program performance, this topic will introduce how to effectively use cache, how to operate cache, optimize memory access speed, how to use less memory, how to prevent memory jitter, memory alignment and so on.
5. Multi-thread optimization topic: how to make full use of CPU, how to do thread synchronization, how to use locks, what locks to use, understanding and debugging context switching, how to make critical threads run more efficiently, and so on.
6. Out-of-order execution project: understand CPU's out-of-order execution strategy and how to use this feature to write efficient code.
7. Vector operation project: understand the vector operation of CPU and various instruction sets, registers and built-in functions to improve the efficiency of data calculation.
8. Special optimized coding skills: here we will introduce many efficient coding techniques, how to reduce the branch prediction rate, how to optimize inefficient division operations, how to better check data boundaries, introduce some efficient mathematical function libraries, and so on.
9. Coding skills of template metaprogramming: how to use template programming to optimize the running efficiency of the program.
10. Hodgepodge: other practical tips.
At this point, I would like to recommend a useful website link to you. The first one is: https://godbolt.org/
Click to view high-definition pictures
As shown in the figure, enter the C++ code, and the compiled assembly code will be displayed in the right half. By looking at the assembly instructions behind the high-level language, we can better analyze the performance of the code. A more powerful feature of the website is that it supports almost all compilers on the market, and there are all versions.
As shown in the picture, the right half of the website will show the code in the eyes of the compiler, which is also very helpful for us to analyze the program.
Thank you for your reading, the above is the content of "what is the optimization of C++"? after the study of this article, I believe you have a deeper understanding of the optimization of C++. The specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.