In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
C++ statement placed the correct application is how, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Since the C++ programming language is regarded as an upgraded version of the C language, it must have many functions of the C language. However, there are still many features that are more optimized than the C language. For example, some of the contents of the C++ statement. The placement of C++ statements will have a significant impact on performance. Similarly, the choice of postfix and prefix operators can also affect performance. In this section, we focus on four issues: initializing v.s assignments, placing declarations where the program does use them, initializing lists of constructors, and prefix v.s postfix operators.
C++ programming language can support many programming styles and can help developers improve their development efficiency to a great extent. Beginners may also be a little confused about the statements in C++. Here we will introduce in detail the relevant methods for placing C++ statements to facilitate your understanding.
(1) use initialization instead of assignment
In C language, variable declaration is only allowed at the beginning of a function body, while C++ declaration placement can appear anywhere in the program. The purpose of this is to delay the declaration of the object until it is actually used. This can have two benefits:
1. Ensures that the object will not be maliciously modified by other parts of the program before it is used. Such a guarantee cannot be made if the object is declared at the beginning but is not used until 20 lines later.
two。 It gives us the opportunity to achieve performance improvement by using initialization instead of assignment. In the past, the declaration could only be put at the beginning, but often at the beginning we did not get the value we wanted, so the benefits of initialization could not be applied. But now we can initialize it directly when we get the value we want, saving us a step.
Note that there may be no difference between initialization and assignment for basic types, but there is a significant difference for user-defined types because the assignment makes one more function call-operator =. So when we choose between assignment and initialization, initialization should be our *.
(2) C++ statement should be placed in the right place
On some occasions, we should pay enough attention to the performance improvement brought about by moving the declaration to the right location. For example:
Bool is_C_Needed (); void use () {C C1; if (is_C_Needed () = = false) {return; / / C1 was not needed} / / use C1 here return;}
In the above code, the object C1 is created even if it is possible not to use it, so we will pay unnecessary money for it, and you may say how much time an object C1 can waste. But what if this is the case: C1 [1000]; I don't think it's wasteful. But we can change this by moving the location of declaration C1:
Void use () {if (is_C_Needed () = = false) {return; / / C1 was not needed} C C1; / / moved from the block's beginning / / use C1 here return;}
So, has the performance of the program been greatly improved? So please analyze your code carefully, the benefits of C++ statement placement are unimaginable.
(3) initialize the list
As we all know, initialization lists are typically used to initialize const or reference data members. But because of its own nature, we can improve performance by using initialization lists. Let's first take a look at a program:
Class Person {private: C / 2; public: Person (const C & C1, const C & c2): C / C 1 (C1), C / C / 2 (c / 2) {}}
Of course, we can also write the constructor like this:
Person::Person (const C & C1, const C & c2) {cyste1 = C1; cym2 = c2;}
So what kind of performance difference will the two bring? in order to figure out this problem, we first need to figure out how the two are executed. let's first look at the initialization list: the declaration operation of the data member is completed before the constructor is executed, and the assignment operation is often completed in the constructor, but the initialization list is initialized directly when the data member is declared. So it only executes the copy constructor once. Let's take a look at the assignment in the constructor: first, the data members are created through default constructor before the constructor is executed, and then assigned through operator = in the constructor. So it makes one more function call than the initialization list. The performance difference comes out. Note, however, that if your data members are basic types, do not use initialization lists for readability, because the compiler generates the same assembly code for both.
(4) postfix VS prefix operator
The prefix operators + + and-are more efficient than their postfix version, because when the postfix operator is used, a temporary object is required to hold the changed value. For basic types, the compiler eliminates this extra copy, but for user-defined types, this seems impossible. So please use the prefix operator whenever possible.
After reading the above, have you mastered the C++ statement how to place the correct application? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.