X = x + 1, x + = 1, which of the following is more efficient?

Source: Internet
Author: User
I used to think that xx1, x1, and x are only different in writing representation methods. I didn't expect to study them carefully ....... From the process above, we can see that x is less of a write process than x1, while x1 is less of an addressing process than xx1 (finding the address of x on the left ), therefore, the efficiency of the three statements is xx1x1x.

I used to think that x = x + 1, x + = 1, and x ++ are only different in writing representation methods. I did not expect to study it carefully, but the difference is not small.

X = x + 1 is the lowest because the execution process is as follows:

  1. Read the memory address of the right variable x;
  2. Execute x + 1;
  3. Read the memory address of variable x on the left;
  4. Pass the value on the right to the variable x on the left (the compiler does not think that the memory address of the variable x on both sides is the same ).

First Look for the address on the right side of x, then read the value of x in the address, and then add the value of x to 1 in the register, next, find the address of x on the left (the computer does not know that x on the left is x on the right), and then save the calculation result to the address of x on the left.

X = + 1. the execution process is as follows:

  1. Read the memory address of the right variable x;
  2. Run x = 1;
  3. Pass the obtained value to variable x (because the memory address of variable x has been read ).

Find the address of x, read the value of x in the address, and add the value of x to 1 in the register, then save the calculation result to the address of x (here the computer knows that the address for reading and writing operations is the same ).

X ++ is the highest, and its execution is as follows:

  1. Read the memory address of the right variable x;
  2. Variable x auto-Increment 1.

Find the address of x, read the value of x in the address, and then add the value of 1 to the address.

From the process above, we can see that x ++ is less than x + = 1, x + = 1 is less addressing than x = x + 1 (search for the address of x on the left ), therefore, the efficiency of the three statements is x = x + 1 <x + = 1 <x ++.

This article is available at http://www.nowamagic.net/librarys/veda/detail/616.

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.