From a performance perspective prefix + + and suffix + + (in-depth analysis of ++i and i++) __ Prefix + +

Source: Internet
Author: User
from the performance point of view prefix + + and suffix + + (++i and i++ in-depth analysis)
When you are learning a lot of programming languages, you often encounter the problem of prefix + + and suffix + +. These two operations make it difficult to distinguish, and easy to confuse, here to explain it, we will never forget after reading, of course, here will also from the perspective of performance to see the problem, we are patient to look at it.
Let's take a look at an example.
int i = 0;
int j = 0;
int k = 0;

j = i++;
K = ++i;

printf ("I =%d, j =%d, K =%d.\n", I, J, K);

This example is very common, at most, the representation is different, in VS2013, the result is i = 2, j = 0, k = 2. From this result we can analyze it.
"J = i++;" Analysis: J is equal to 0, because the suffix + +, that is, i++, in fact, is the first assignment after the + + calculation, that is, I initial value is 0, the first 0 assigned to J, then I only + +, at this time j = 0, i = 1.
"k = ++i;" Analysis: K is equal to 2, is because the prefix + +, that is, ++i, in fact, first of all, after the calculation of the value of + +, that is, I was 1, and then the + + operation, I into 2, and then I assigned to K, at this time k = 2, I
The end result is the one seen above. We only need this memory: "prefix + + in front, so should be first + + after the assignment, suffix + + in the post, so it should first assign value after + +". So that we can remember the difference between the two.
So let's look at the difference between these two operations from a performance perspective. When you look at a lot of textbooks, you may often see such an example structure.
for (int i = 0; i < 5; i++)
{
    
}

Basically all textbooks adopt i++ this form to carry out "I increment", in fact at this time i++ and ++i do not have what the difference of operation result, to the result of the program will not have what effect, so use ++i or i++ is same, but many books like directly with i++, Perhaps this form has become a habit of everyone, conveniently picked, So wrote, also did not feel anything wrong, but a lot of professional programmers like to write.
for (int i = 0; i < 5; ++i)
{

}

Why do they like to use this form? The difference between this form and the above is to replace i++ with ++i, in fact, the results of the implementation of the program is exactly the same, in both of the time, the reason for the adoption of ++i instead of i++ because, in the execution of ++i, the bottom directly in the register of I was added 1 operations, and then saved in the register can be, and I + + because you want to save the current value of I, and then add 1, so you need to waste a register to store the current value of I, and then add 1 to the operation and then save the value after 1, so the equivalent of using two registers. Of course, some languages do not necessarily involve the underlying concept, but you should know that i++ performs a more cached operation than ++i, which has little effect on the result, but has some small impact on execution efficiency and wastes some resources to save the current value. Of course, in these cases, the waste of these resources is negligible, but in a large system, it may be necessary to haggle over efficiency, sometimes even 1us is important, so do the bottom of the need to recognize this problem.
In short, we first have to figure out the difference between prefix + + and suffix + +, remember the above sentence is OK. Then, you have to be clear about the weak effect of the two on performance.

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.