I know that ++a is faster than a++ because a++ has to first store the previous value of a at some temporary location and then increment a and then return the saved value, whereas ++a simply increments a and returns the pointer to the incremented value.

Since, both a++ and ++a are using a=a+1 as an intermediate step, wouldn't using a=a+1 itself be faster than a++ or ++a ?(since we're not returning an pointer here)

What about a+=1 ? Is it translated simply to a=a+1 and does this have a speed penalty.

There is a lot of confusion as I see it, on the net. Plz help

>I know that ++a is faster than a++ because a++ has to first store
>the previous value of a at some temporary location and then
>increment a and then return the saved value, whereas ++a simply
>increments a and returns the pointer to the incremented value.

That's a reasonable analysis, though you'll be interested to learn that giving ++a and a++ identical performance properties is an optimization that's been in place since Dennis Ritchie's first C compiler. It's on our list of "if you don't implement this, you fail as a compiler writer" bullet points. I would be appalled if I found a compiler where the difference between pre increment and post increment weren't vanishingly small (if measurable at all), even for cases where the expression is part of a larger expression.

>Is it translated simply to a=a+1 and does this have a speed penalty.
The following statements will generally be translated to identical machine code:

Naive: a = a + 1; Compound: a += 1; Pre increment: ++a; Post increment: a++;

It's on our list of "if you don't implement this, you fail as a compiler writer" bullet points.

I have to disagree here. A performance of pre- vs post- very much depends on the target architecture. For example, cpu32 has (Ax)++ and --(Ax) addressing modes (intended for stack operations), which maps directly to C *ptr++ and *--ptr idioms. By contrast, it has no hardware support for their peers. A compiler writer may of course artificially slow the predec down to match the performance of postdec, but I doubt anybody would do that.

If anyone is worried about which makes his code faster :

A) ++a;
B) a++;
C) a = a + 1;
d) a += 1;

Then I am more worried about his code.

Thanks A lot!

I understand that all 4 are optimized by the compiler to give the same performance. But, in the interviews, etc., they ask the exact order. So, is my reasoning right when i say that, since i=i+1 is involved in i++,++i and i+=1, therefore, i=i+1 must be the fastest, followed by i+=1, followed by ++i followed by i++ ?

Also, is i+=1 translated into i=i+1 ?

Maybe the answer to the interview question is that none is faster than any other? Or that the answer is outside of C itself?

It depends on what "i" is. If its a class then there if no way of knowing
exactly what it does unless you see the code for it. If "i" is a primitive type data, then look at its assembly code and see which one is the fastest. It might vary with compilers.

class

It's a C forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.