-3

Currently I'm studying c from the c primer. It is written that use of ++ operator increases the efficiency of the program, because it is similar to machine level language.

So I want to know that how this is similar to machine level language moreover it is different than previously asked question which is asked about add operations

  • 4
    1. C and C++ are separate languages 2. Where did you read that? Unless you have a very old/inefficient compiler it is usually wrong – UnholySheep May 21 '17 at 10:18
  • Hyi...thnks for mentioning point 1.secondly it has also mentioned that new smart compilers change the ++x to x=×+1 so it reduces the efficiency . I know you are right but i just want to know how the old compiler performed better by using ++ operator – user8043557 May 21 '17 at 10:24
  • 2
    You still need to explain where you get that information from (who wrote it, when was it published, ...). However very, very old compilers simply performed nearly 1:1 mapping of C to assembly, so `++` operators were mapped to e.g.: `INC` instructions in assembly (while `x=x+1` might have been turned into multiple instructions). Modern compilers are way better at optimizing code and do that in both cases. – UnholySheep May 21 '17 at 10:26
  • I read it from c primer plus Stephen prata 6th edition – user8043557 May 21 '17 at 10:28
  • 2013? Surprisingly recent. – melpomene May 21 '17 at 10:29
  • 2
    Quote from the 5th edition (2004): "*Another advantage of the increment operator is that it usually produces slightly more efficient machine language code because it is similar to actual machine language instructions. However, as vendors produce better C compilers, this advantage may disappear. A smart compiler can recognize that `x = x + 1` can be treated the same as `++x`.*" – melpomene May 21 '17 at 10:33
  • Always remember that [the optimizer is smarter than you think](https://godbolt.org/g/fOUPx5). – Quentin May 21 '17 at 10:44

2 Answers2

3

Nowadays it does not matter, i.e. a C statement

i++;

is the same in terms of efficiency as

i += 1;

or

i = i + 1;

Modern compilers are perfectly aware that all the above statements are the same and use the most efficient CPU instructions.

Andriy Berestovskyy
  • 8,059
  • 3
  • 17
  • 33
0

With modern tools and compilers there's no reason to assume one is any better than another. It's "syntactic candy." If you want to make sure, disassemble your code and examine the differences. I disassemble often to examine it in the world of microcontrollers, where every clock cycle counts (and can be counted) and I've never seen one choice or the other change performance one bit.

However ++x and x++ might be different depending on conditions, as ++x increments X before its next use and x++ increments it after:

int x = 0;
while (x++ < 10) { do something }

Will behave differently than

int x = 0;
while (++x < 10) { do something}

But as far as performance goes, using modern tools, all methods to increment x by one are probably the same.

TomServo
  • 7,248
  • 5
  • 30
  • 47
  • Wording: `++x` first increments `x` and the returns the new value, `x++` returns the value of `x` and then increments it (saying *"before its next use"* sounds confusing) – UnholySheep May 21 '17 at 10:34