Read two books on the performance optimization of C + + programs __c++

Source: Internet
Author: User

Recently in a temper, saw most of the "C + + Application performance optimization" and "efficient C + + performance programming techniques." The overall feeling, although the content is different, but the former is like the latter's culture version and for VC2003 version. To think of Chinese should be concise, but the former seems to be a bit long-winded, looking at the opposite of the smooth.

Always use C + +, most of the time to write numerical calculation program, so the performance of C + + often hung in the heart. The performance comparison of various languages, the comparison of various compilers, looked at the comparison of the study, the final conclusion is nothing but that few seemingly no profound principle of the experience of the nature of the description. When escaping from the BBS on the right and wrong place, calm down to pay attention to a specific detail, only to find their understanding is how much superficial, hearsay of things, not to do the letter.

Virtual functions have been thought to be the primary source of performance degradation in Oo, and template and model programming are the only glimmers of efficiency. But the real situation tells me that virtual tables and virtual methods are found, does not allow programs to degrade too much performance, which is a good language feature for programming versatility, scalability, reusability, and performance; "Where to reduce performance", to be exact, "where the program is not going to improve performance further." Is that dynamic binding lets the compiler not inline the method, thereby losing the performance improvement that the inline brings.

Also thought that inline is the method in the code moved over, no register to save, no call, so there is no overhead of these calls. But the fact is that this is just the basic benefit of inline, and the more benefit is that the code moves to the place where the call is made, allowing the compiler to perform a comprehensive optimization based on the context.

For example, this code: Class gatedint ... {
int x;
Public
int get () ... {
return x;
}
void set (int arg) ... {
x = arg;
}
}

int main ()
... {
Gatedint gi;
Gi.set (12);
cout << gi.get ();
}

After inline, it will be optimized to: int main ()
... {
cout << 12;
}

Inline is called a C + + program to get the easiest way to improve performance. In practice, however, the criteria for inline are more complex. There is a tendency to say that inline or not is something that the compiler should set up for itself, and one day we will ignore inline.

Now finally understand, if can really see the C + + code each sentence, corresponding will produce how many lines of life, optimization performance is not a problem. The fact is that in addition to compilers, people are hard to do this. So only to sum up the points, in order to constantly remind themselves, pay attention to each line of code you write, what exactly do things:
Constructors and destructors. Building objects, just a simple sentence. Object obj or Object *pobj = new object. However, it is a question of how many time-consuming operations have been done in construction and destructor. In addition, in the constructor, the order of the initial object is determined solely by the order of the members ' declarations, regardless of the initial render list. But it's a good habit to use a first-order list. A temporary variable. The emergence of temporary variables is difficult to find, the most typical is the code of matrix addition: matrices A,b,c; C=a+b: There is no knowing how many people can clearly point out the number of temporary variables in the operation. The return object is a common condition for generating temporary variables, but fortunately there is a "return value optimization" method that can reduce the generation of temporary variables to some extent. New/delete, the default library unless you really understand it, otherwise it is not considered for performance, even if it is default. New/delete is thread-safe. But in some cases, this is not necessary. After the lock is removed, it is easy to see that the program performance can be greatly improved. Heaps and stacks. The stack has CPU-level operation instruction support, so the speed is very fast, and the heap needs the usual algorithm to maintain, and new/delete implementation. So it's slower. Do not fully believe in STL. However, it is helpful to study more about the effective STL.

Generally speaking, there are not many ways to optimize the performance of a program. But writing an efficient program is still not an easy thing to grasp. Have seen a netizen through their own experiments to verify the performance of i++ and ++i no big different, boldly suspected "++i" this experience. Now it looks really funny. Every time you read a book, you will find that in fact, as long as you browse the last section of each chapter, the approximate content will be able to understand the heart. But if only browsing these key points, as in the BBS to see other people experience paste, only will produce immature suspicion or understanding, a little deeper, they will find their superficial and impetuous.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.