tags:

views:

399

answers:

10

Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?

In terms of usage of the following, please rate in terms of execution time in C. In some interviews i was asked which shall i use among these variations and why.

a++
++a
a=a+1
a+=1
+55  A: 

Here is what g++ -S produces:

void irrelevant_low_level_worries()
{
    int a = 0;
//  movl    $0, -4(%ebp)

    a++;
//  incl    -4(%ebp)

    ++a;
//  incl    -4(%ebp)

    a = a + 1;
//  incl    -4(%ebp)

    a += 1;
//  incl    -4(%ebp)
}

So even without any optimizer switches, all four statements compile to the exact same machine code.

FredOverflow
+1 for the function name
Bob
@Bob: I second that.
FrustratedWithFormsDesigner
@NullUserException: Err, no, he turned optimization off. +1
Billy ONeal
Make `a` a global variable and then it won't be optimized away.
wilhelmtell
@Null: a is being incremented, so that is not optimized away. if (a++) or (++a) is used, then it's a different situation and can't be compared. This question is mostly for "for (int i = 0; i < 10; ++i) { ... }"
stefaanv
@Null: Are you talking about using `a++` vs. `++a` as part of a larger expression? But then it is irrelevant to measure the performance, because the two expressions yield different results...
FredOverflow
Fred, we could make them comparable by using `<` in one comparison and `<=` in the other.
Steven Sudit
@Fred Fair enough ;)
NullUserException
A: 

Well, you could argue that a++ is short and to the point. It can only increment a by one, but the notation is very well understood. a=a+1 is a little more verbose (not a big deal, unless you have variablesWithGratuitouslyLongNames), but some might argue it's more "flexible" because you can replace the 1 or either of the a's to change the expression. a+=1 is maybe not as flexible as the other two but is a little more clear, in the sense that you can change the increment amount. ++a is different from a++ and some would argue against it because it's not always clear to people who don't use it often.

In terms of efficiency, I think most modern compilers will produce the same code for all of these but I could be mistaken. Really, you'd have to run your code with all variations and measure which performs best.

(assuming that a is an integer)

FrustratedWithFormsDesigner
A: 

I can't see why there should be any difference in execution time, but let's prove me wrong.

a++

and

++a

are not the same however, but this is not related to efficiency.

When it comes to performance of individual lines, context is always important, and guessing is not a good idea. Test and measure is better

shodanex
Yes, it is related to efficiency. With `++a`, we just increment a and return the new value. With `a++`, either we have to store `a` into a register, then increment `a`, and then return the value in the register, or we have to increment `a` and return the new value minus `1`. This is obviously optimized away if you aren't using the return value.
mathepic
+5  A: 

You can't rate the execution time in C, because it's not the C code that is executed. You have to profile the executable code compiled with a specific compiler running on a specific computer to get a rating.

Also, rating a single operation doesn't give you something that you can really use. Todays processors execute several instructions in parallel, so the efficiency of an operation relies very much on how well it can be paired with the instructions in the surrounding code.

So, if you really need to use the one that has the best performance, you have to profile the code. Otherwise (which is about 98% of the time) you should use the one that is most readable and best conveys what the code is doing.

Guffa
Translation: This is a stupid question, to which there is no meaningful answer.
Steven Sudit
@Steven Sudit: Not a stupid question, but pointless and outdated. The question is based on conditions that were relevant 10 years ago, but today's hardware and programming culture puts completely different focus on how you write code.
Guffa
I see your point, but I'm not sure I agree. Even in the days of poorly-optimizing C compilers and processor speeds in the low MHz range, this question didn't have much value.
Steven Sudit
@Steven Sudit: Yes, you are right that the question was never of great value, but once upon a time it was at least possible to determine exactly how long each instruction would take on a given computer.
Guffa
That's true. Now, as you pointed out, we can only discuss the average time when in a given context, and then only on a given computer. I used to be able to rattle off how many (best-case) clock cycles an operation would take, but that's become somewhat pointless.
Steven Sudit
A: 

In an interview, I would go with two answers:

  1. At first glance, the generated code should be very similar, especially if a is an integer.
  2. If execution time was definitely a known problem - you have to measure it using some kind of profiler.
Jeff
Id modify 2) to 2') : If execution time was definetly a known problem, most likely the problem is somewhere else. So do 2)
Tom
+1  A: 

This depends on the type of a as well as on the context of execution. If a is of a primitive type and if all four statements have the same identical effect then these should all be equivalent and identical in terms of efficiency. That is, the compiler should be smart enough to translate them into the same optimized machine code. Granted, that is not a requirement, but if it's not the case with your compiler then that is a good sign to start looking for a better compiler.

wilhelmtell
+2  A: 

The circumstances where these kinds of things actually matter is very rare and few in between. Most of the time, it doesn't matter at all. In fact I'm willing to bet that this is the case for you.

What is true for one language/compiler/architecture may not be true for others. And really, the fact is irrelevant in the bigger picture anyway. Knowing these things do not make you a better programmer.

You should study algorithms, data structures, asymptotic analysis, clean and readable coding style, programming paradigms, etc. Those skills are a lot more important in producing performant and manageable code than knowing these kinds of low-level details.

Do not optimize prematurely, but also, do not micro-optimize. Look for the big picture optimizations.

polygenelubricants
+1  A: 

For most compilers it should compile to the same ASM code.

vash47
A: 

It depends on the context, and if we are in C or C++. In C the code you posted (except for a-- :-) will cause a modern C compiler to produce exactly the same code. But by a very high chance the expected answer is that a++ is the fastest one and a=a+1 the slowest, since ancient compilers relied on the user to perform such optimizations.

In C++ it depends of the type of a. When a is a numeric type, it acts the same way as in C, which means a++, a+=1 and a=a+1 generate the same code. When a is a object, it depends if any operator (++, + and =) is overloaded, since then the overloaded operator of the a object is called.

Also when you work in a field with very special compilers (like microcontrollers or embedded systems) these compilers can behave very differently on each of these input variations.

Rudi
Why would `a++` be faster than `++a`? :)
FredOverflow
@FredOverflow, Thats correct. The answer is wrong. `++a` will always be at least as fast as `a++`, faster if you need the return value.
mathepic
@FredOverflow It depends on the opinion of the interviewer.
Rudi
@FredOverflow, when `a` is a pointer, the full expression is `total += *(a++)` or similar, and you are on a processor that has a post-incrementing indirect addressing mode but no pre-incrementing one. e.g. M68000. Yes, in the olden days, we used to use `*(p++)` and `*(--p)`, but only very infrequently `*(++p)` or `*(p--)`, as those matched the addressing modes available in hardware. (And in fact, those two happen to match what you usually want to do with a pointer to a NUL-terminated string or other sentinel-terminated list, which is why they're the ones implemented in hardware.)
John Marshall
-1 for misleading statement that there's a high chance of performance being different.
R..