C + + interview question: ++i and i++ which is high efficiency?

Source: Internet
Author: User

1, when the data type of the variable i is the C + + language default provided by the type, they are the same efficiency.

The number of bars executed from its assembly is the same, so its execution efficiency is the same (interested in using GDB to view the assembly code)

2, our custom data type, ++i efficiency is higher than i++, through operator overloading to give you this point.

Operator operator::operator++ ()
{
++value; Internal member variables
return *this;
}

Operator operator::operator++ (int)
{
Operator temp;
Temp.value=value;
value++;
return temp;
}

After + + must have a temporary object can be completed, the creation of temporary objects, a series of copy action needs to occur, efficiency is naturally much lower than the direct reference object.

C + + interview question: ++i and i++ which is high efficiency?

Related Article

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.