4

I am curious to know the computing speed difference between n++ and n=n+1 in C. I found a rather lengthy discussion on Stack Overflow, where some people are literally contradicting each other.

To summarize briefly for the nonspecialist, my interpretation is the following:

  1. With modern compilers the speed difference is not worth worrying about.
  2. Most people these days use the suffix increment operator n++ and not n=n+1.

Am I right?

2
  • Am I right? uma1966
    – uma1966
    Jul 10 '14 at 22:41
  • Well, I agree with the opinion that says no difference at all except that n++ is shorter, faster to right and looks good!
    – kzidane
    Jul 10 '14 at 22:43
2

Actual compilers perform a lot of optimizations while compiling the code.

Older compilers and processors treated n++ and n=n+1 in different ways, producing a faster code for the first expression using register access instead of memory access.

2
  • 1
    What about the precedence of prefix and postfix increment/decrement operator is greater than addition(+) and subtraction(-)?
    – sinister
    Aug 2 '14 at 3:06
  • 1
    Yes, the precedence is greater, but it does not affect the speed of the code.
    – Cygni_61
    Aug 2 '14 at 7:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .