0

Is there is any difference between these two statements performance wise ?

i++;
i = i + 1;
Ahmed
  • 59
  • 7

2 Answers2

7

Depends on the optimisation. i++ can, on most processors, be represented as a single machine language instruction. i = i + 1, on the other hand, could be represented by up to four: load i, load 1, add, store to i; although, even a middling smart compiler should be able to recognise it can rewrite it into the former.

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

no, there is no difference. The compiler will compile these into the same assembly

Stephen
  • 4,041
  • 22
  • 39
  • 1
    Yes, Amadan's answer is more complete and correct. I did assume that the code was compiled with optimization enabled. – Stephen Aug 29 '14 at 05:10
  • 1
    @DividebyZero: Well, to be honest, today in the age of LLVM and stuff, there actually is no difference, I believe. It's just that I don't automatically assume C = modern C :) (I have some acquaintances who still work on 25-year-old Sun boxen) – Amadan Aug 29 '14 at 05:14