Reputation: 36661
This was my Interview question in HP. I answered a++ takes less instruction compared to a = a +1;
I want to know which is useful in efficient programming, and how both are different from each other..?
Hoping for quick and positive response..
Upvotes: 7
Views: 11945
Reputation: 46037
I am not an expert in microprocessor design, but I guess many processors have a INC or DEC instruction. If the data type is int then increment can be done in one instruction. But a = a + 1 requires more, first add and then assignment. So a++ should be faster, obviously assuming that a is not a complex data type.
However a smart compile should do this kind of optimization.
Upvotes: 4
Reputation: 894
++a;
a+=1;
a=a+1;
Which notation should we use? Why?
We prefer the first version, ++a, because it more directly expresses the idea of incrementing. It says what we want to do (increment a) rather than how to do it.  (add 1 to a and then write the result to a).
In general, a way of saying something in a program is better than another if it more directly expresses an idea.
The result is more concise and easier for a reader to understand. If we wrote a=a+1, a reader could easily wonder whether we really meant to increment by 1.
Maybe we just mistyped a=b+1, a=a+2, or even a=a–1.
With ++a there are far fewer opportunities for such doubts.
Note: This is a logical argument about readability and correctness, not an argument about efficiency. Contrary to popular belief. Modern compilers tend to generate exactly the same code from a=a+1 as for ++a when a is one of the built-in types.
Upvotes: -1
Reputation: 11
a++ is better than a+1 because in the case of floating point numbers a++ increments more efficiently than a=a+1. I.e. a++ increments exactly 1 and no rounding takes place.
Upvotes: -9
Reputation: 19344
Really it all boils down to what your compiler does optimize.
Lets take the optimal case of a is an int. Then normally your compiler will make a++
and a=a+1
be exactly the same.
Now what can be pointed out, is that a = a + 1;
is purely incrementing the value of the fixed amount 1,
whereas a++
is incrementing the value of 1 of the type of the variable. So if it is an int, float etc you'll get 1->2, 3.4->4.4 in both cases.
But if a
is a pointer to a array/list etc, you'll be able to change pointer to the next element in the list/array when using a++
, while a = a+1
might do something else or not work at all.
Long answer short, I'd say a++
is better:
++
is a basic operator on the same level as <<
etc.: it modifies directly the variable, while a = a + 1
, if not optimized by your compiler, will require more operations by adding a with another number.Upvotes: 0
Reputation: 47193
In C, there would be no difference, if the compiler is smart.
In C++, it depends on what type a
is, and whether the ++
operator is overloaded. To complicate matters even more, the =
operator can be overloaded too, and a = a + 1
might not be the same as a++
. For even more complication, the +
operator can also be overloaded, so an innocent looking piece of code such as a = a + 1
might have drastic implications.
So, without some context, you simply cannot know.
Upvotes: 19
Reputation: 34036
from http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15 :
++i is sometimes faster than, and is never slower than, i++.
Upvotes: -4
Reputation: 507115
I think I would answer in an implementation independent way. The a++
is easier to read to me because it's just showing me what it does. Whereas for a = a + 1
I first have to scan all the addition. I prefer to go for the choice that's more foolproof.
The former, a++
, evaluates to the prior value, so you can use it to express things in sometimes surprisingly simpler manners. For instance
// copy, until '\0' is hit.
while(*dest++ = *source++) ;
Apart from these considerations, I don't think any of them is more efficient, assuming you have to do with basic integer types.
Upvotes: 4
Reputation: 31445
Even more efficient in many cases in ++a. When a is an int or a pointer though it is not going to make any difference.
The rationale of why these increments are more efficient though than a=a+1
is because the instruction of increment is one instruction whereas the instructions involved in adding 1 to a then assigning it back is something like:
get the address of a push its contents onto the stack push 1 to the stack add them get the address of a (possibly already stored) write (pop) from the stack into this address
Upvotes: 0
Reputation: 4698
As far as I know, there's no difference between a++
and a = a + 1.
HOWEVER, there is a difference between ++a
and a = a + 1
Let's take the first case, a = a + 1
.
a = a + 1
will have to take the value of a, add one to it, and then store the result back to a.
++a
will be a single assembly instruction.
You can notice the difference with these two examples:
Example 1
int a = 1;
int x = a++; //x will be 1
Example 2
int a = 1;
int x = ++a; //x will be 2
BE AWARE! Most compilers optimize this today. If you have a++ somewhere in your code it will MOST likely be optimized to a single assembly instruction.
Upvotes: 0
Reputation: 1660
With an optimizing compiler they are identical. The interview question is moot.
Upvotes: 1
Reputation: 170509
First of all, in C++ this will depend on type of a
. Clearly a
can be of class type and have those operators overloaded and without knowing the details it's impossible to decide which is more efficient.
That said, both in C and C++ whatever looks cleaner is preferable. First write clear code, then profile it and see if it's intolerably slow.
Upvotes: 7