-3

In languages like C++, there is an expression like x += y.

If x had already been occupied by a specific floating number (e.g. 3.0), is there any FLOP count difference between the following two?

    1. x += 1.0
    1. x = x + 1.0

Edited

To be specific, in languages such as Python, there is indeed the difference between

  • x+= 1.0
  • x = x+1.0

where the first indicates the modification of the data-structure itself, and the second corresponds to the reassignment of the variable. (Reference : What is the difference between i=i+1 and i+=1 in a for loop?

For FLOP counts, one may be interested in the following two things:

  • the number of +s
  • the number of *s

so it might be natural for a novice to C++ as me to wonder about the difference between the FLOP counts of the two.

As the answerer points out, the difference can show up even in C++ when we use volatile objects.

moreblue
  • 322
  • 1
  • 4
  • 16

1 Answers1

3

C and C++ do not say anything about CPU operations. Both languages specify the behavior of the expressions, not how they're implemented.

The behavior of x = x + 1.0 and x += 1.0 are identical, except that any side effects of evaluating x occur twice in the former and once in the later. If x is the name of a non-volatile object, then there is no semantic difference, and any decent compiler should generate exactly the same code for both.

Differences can show up if you write something like:

arr[func()] = arr[func()] + 1.0;

vs

arr[func()] ++;
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631