Which of the following is the high efficiency of ++ I and I ++?

Source: Internet
Author: User

This issue needs to be explained in two cases: 1. When the Data Type of variable I is the default type provided by the c ++ language, their efficiency is the same. Int a, I = 0; a = ++ I; assembly code: int a, I = 0; 01221A4E mov dword ptr [I], 0 a = ++ I; 01221A55 mov eax, dword ptr [I] 01221A58 add eax, 1 01221A5B mov dword ptr [I], eax 01221A5E mov ecx, dword ptr [I] 01221A61 mov dword ptr [a], ecx int a, I = 0; a = I ++; the Assembly Code is as follows: int a, I = 0; 009E1A4E mov dword ptr [I], 0 a = I ++; 009E1A55 mov eax, dword ptr [I] 009E1A58 mov dword ptr [a], eax 009E1A5B mov ecx, dword ptr [I] 009E1A5E add ecx, 1 009E1 A61 mov dword ptr [I], ecx can be seen from the assembly code that their execution lines are the same! 2. We customize the data type, and the efficiency of ++ I is higher than that of I ++. We will illustrate this through Operator overloading. Operator: operator ++ () {++ value; // internal member variable return * this;} Operator: operator ++ (int) {Operator temp; temp. value = value; value ++; return temp;} Have you seen it? And must have a temporary object. So his efficiency naturally drops!

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.