A ++ vs a = a + 1, which is useful in efficient memory programming, and how?

That was my question for an interview at HP. I replied that ++ takes fewer instructions than a = a +1;

I want to know what is useful in efficient programming and how are both different from each other.?

Waiting for a quick and positive response.

+4
source share
11 answers

There will be no difference in C if the compiler is smart.

In C ++, this depends on the type of a and on whether the ++ operator is overloaded. To complicate matters, the = operator can also be overloaded, and a = a + 1 may not be the same as a++ . For a more complex larger operator, + can also be overloaded, so an innocent looking piece of code, such as a = a + 1 , can have serious consequences.

So, without any context, you simply cannot know.

+18
source

First of all, in C ++ this will depend on type a . Obviously, a can have a class type and overload these operators and not know the details that cannot be solved, which is more efficient.

However, in both C and C ++, anything that looks cleaner is preferable. First write a clean code, then profile it and see if it is too slow.

+7
source

I think I would respond to an independent implementation. a++ easier to read for me because it just shows me what it does. While for a = a + 1 I first need to scan the entire add-on. I prefer to choose which will be more reliable.

The first, a++ , evaluates the previous value, so you can use it to express things at times in a surprisingly simple way. For instance,

 // copy, until '\0' is hit. while(*dest++ = *source++) ; 

Beyond these considerations, I don’t think any of them are more efficient, assuming you are dealing with basic integer types.

+4
source

I am not an expert in microprocessor development, but I think that many processors have an INC or DEC instruction. If the data type is int, then the increment can be performed in one instruction. But a = a + 1 requires more, first adds and then assigns. So ++ should be faster, obviously, assuming a is not a complex data type.

However, the smart compiler must do this optimization. So, summer, this is a difficult interview or exam question, with almost no real life value, if you are not working with a not-so-smart compiler.

+2
source

They are identical with the optimizing compiler. The interview question is controversial.

+1
source

As far as I know, there is no difference between a++ and a = a + 1.

HOWEVER, there is a difference between ++a and a = a + 1

Take the first case, a = a + 1 .

a = a + 1 should take the value a, add a value to it, and then save the result back.

++a will be one build command.

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 

KNOW! Most compilers are optimizing this today. If you have ++ somewhere in your code, MOST will probably be optimized for one build instruction.

0
source

Even more efficient in many cases in ++ a. When a is an int or pointer, although that will not make any difference.

The explanation of why these increments are more efficient than a=a+1 is that the increment command is one command, while the instructions for adding 1 to the assignment are then assigned:

get the address pull its contents on the stack insert 1 on the stack add them get the address (possibly already saved) write (pop) from the stack to this address

0
source

In fact, it all comes down to what your compiler optimizes.

Let's look at the optimal case of a as int. Then usually your compiler will make a++ and a=a+1 exactly the same.

Now we can indicate that a = a + 1; purely increases the value of the fixed sum 1, while a++ increases the value 1 of the variable type. Therefore, if it is int, float, etc., you will get 1-> 2, 3.4-> 4.4 in both cases.

But if a is a pointer to an array / list, etc., you can change the pointer to the next element in the list / array when using a++ , while a = a+1 may do something else or not work at all.


The long answer is short, I would say a++ better:

  • your code becomes clearer and shorter
  • You can manipulate a wider range of varaible types.
  • and should be more efficient since (I think, but I'm not sure) ++ is a basic operator at the same level as << , etc.: it changes the variable directly, and a = a + 1 if no optimized by your compiler, will require more operations by adding another number.
0
source
 ++a; a+=1; a=a+1; 

What designations should we use? What for?

We prefer the first version of ++ a because it more directly expresses the idea of ​​magnification. It says what we want to do (increment a), and not how to do it.  (add 1 to a, and then write the result on a).

In general, a way to say something in a program is better than another if it expresses the idea more directly.

The result is more understandable and understandable to the reader. If we wrote a = a + 1, the reader could easily wonder if we should really increase by 1.

Perhaps we just masked a = b + 1 , a = a + 2, or even a = a-1 .

C ++ a is much less likely to have such doubts.

Note. This is a logical argument about readability and correctness, not an argument about efficiency. Contrary to popular belief. Modern compilers, as a rule, generate exactly the same code from a = a + 1 , as for ++ a , when one of the built-in types.

-1
source

from http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15 :

++ I am sometimes faster than, and never slower than, I ++.

-3
source

a ++ is better than a + 1, because in the case of floating-point numbers, a ++ increases more efficiently than a = a + 1. That is, a ++ increases exactly 1, and rounding does not occur.

-eleven
source

Source: https://habr.com/ru/post/1332462/


All Articles