i++ and ++i efficiency

Source: Internet
Author: User

Actually this problem, Baidu words, there is a lot of reference, but, here, I produced some confusion, they analyzed the results, and my test code inconsistent, this let me tangled, so, once again write this question, Shun shun thinking.

My test Environment : System: Windows7 32bit, programming Platform: Qt 5.3.1 (MSVC 2010,32bit)

First of all, let's talk about the results that are accepted by the public:

(1) At the time of the custom data type, because the prefix (++i) can return the object's reference, the suffix (i++), must return the object's numeric value, therefore, causes the large object to produce the large replication overhead, causes the efficiency to reduce. Therefore, using the custom data type, the use of prefix (++i) more efficient;

(2) If it is a built-in type, such as int, the efficiency is the same.

(3) C++primer above said: For the old compiler ++i efficiency is good, for the good compiler i++ is optimized (the same efficiency). It means which one to use.

My test code :

#include <iostream>using namespacestd;//struct (1 data member)structoj{intJ;} A;//(2 data members) classclassoo{ Public: oo (): A (0), B (0) {} oooperator++()//Prefix plus    {        ++A; ++b; return* This; } oooperator++(int)//suffix plus{oo temp (* This); A++; b++; returntemp; }Private:    intA; intb;};intMain () {intI=0; intx=0; //Built-in data Typesi++; ++i; X=i++; X=++i; //struct (1 data member)A.J =0; A.I=0; A.J++; ++A.J; X= a.j++; X= ++A.J; //(2 data members) classOo O,ox; o++; ++N; Ox= o++; Ox= ++o; return 0;}

The closest assembly to machine language

(1) analyze the built-in data types first :

The analysis results are as follows:

For built-in data types, using i++ and ++i alone is inefficient. However, i++ and ++i in an expression are different, as shown, so the efficiency of the expression containing ++i is better for the built-in expression, depending on the requirements. (Note that I emphasize the efficiency of the whole expression here.) Rather than pure i++ and ++i.)

(2) custom data type (struct):

Like the test code, the struct is used here and contains a data member.

The analysis results are as follows:

For a struct with a custom data type that contains one data member, the efficiency of the separate a.j++ and + + A.J is the same, and there is nothing to argue about. However, for the expression in x = a.j++ and x = ++A.J, the efficiency of the expression, the result does appear and "Custom data type, ++i more efficient" violation. The reason picture has been explained, so, for the expression containing the self-increment and the decrement operator, when discussing the efficiency of the expression, can not be used purely theoretical basis, but also to consider their own platform environment, compiler instruction set system, here I think of the simplified instruction set and complex instruction set.

(3) Custom data type (class, 2 data members, overloaded + + operator)

Analysis results:

For the self-increment operation of the class object, we overloaded the operator. From the assembly code point of view, whether it is used alone, or the expression containing the increment operator, o++ operation, because there is the stack and out of the stack operation, it seems inefficient. However, in this case, we are not considering the efficiency of the overloaded function at all, but this shows the invocation of the function. In fact, there is no need to delve into the efficiency of overloaded functions, single from the C + + language can be seen. The ++i of the prefix is more efficient than the overloaded functions.

The assembly code comparison of 2 overloaded functions (the overload with the ++i prefix on the left and the i++ suffix on the right):

Self-summary :

Here, I do not want to say, i++ and ++i efficiency which is better, through the above test code, we can find that for different environments, different data structures will have different efficiency. What I want to say is that I learned a lot by discovering a problem, then finding the answer, questioning the answer, and validating the answer to the process. Perhaps my verification method is unscientific, but from which I seriously treated, from which, learned to read the assembly code, which makes me more profound understanding of the machine for the operation of the program is what kind of mechanism. Take the problem seriously and you will reap a lot of it.

If an interviewer asks you such a question, you can answer the following:

Built-in data types are as efficient as they are.
However, at the time of the custom data type, since the prefix (++i) can return the object's reference, the i++ must return the value of the object
Therefore, the result is a large replication overhead, resulting in reduced efficiency.
So, using a custom data type, use a prefix (++i);

At the same time, you really need to improve the efficiency of your program in detail, you can write a simple test case, test it. Because the test on their own platform, relatively accurate some. No need, like I'm so tangled.

i++ and ++i efficiency

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.