About the efficiency of C + +

Source: Internet
Author: User

Recently read, feel C + + two points to compare the impact of efficiency

1, is the construction and destruction of temporary objects. In order to avoid the creation of temporary objects, C + + compiler has done a lot of optimizations. such as the initialization list of the object's constructors, and the NRV optimization,

2, Class TCLASS3, {4, Public:5, Tclass (): Temp ("") 6, {7, 8,}9, 10, String temp;11,};

When you use the initialization list, the compiler avoids the creation of a temporary object when the constructor is generated. Without initializing the table, the compiler generates constructors as follows:

Class Tclass{public:tclass ():d ata ("") {   String temp = "";   data = temp;} string data;};

NRV optimization refers to the optimizations that the compiler makes to code when the object is returned as a function value, before the compiler optimizes the code

void Fun () {  string temp;  temp = Test ();} String Test () {string temp; return temp}

This is done after the optimization:

12, void Fun () 13, {14,  string temp;15,  temp = Test (), 16,}17, void Test (string& temp) 18, {19, 20, return; 21,}

The premise of NRV optimization is that the object needs to provide a copy constructor to activate the NRV optimization in the compiler when the copy constructor is provided

There is also a virtual function caused by the dynamic binding, virtual function of the formation of the polymorphic mechanism, so that the program only in the run time to determine the specific function is called, this will also affect the efficiency of some degree.

About the efficiency of C + +

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.