开发者

rate ++a,a++,a=a+1 and a+=1 in terms of execution efficiency in C.Assume gcc to be the compiler [duplicate]

开发者 https://www.devze.com 2023-01-12 17:35 出处:网络
This question already has answers here: 开发者_开发百科 Closed 12 years ago. Possible Duplicate: Is there a performance difference between i++ and ++i in C++?
This question already has answers here: 开发者_开发百科 Closed 12 years ago.

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


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.


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.


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.


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.


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


Same.

For more details see http://www.linux-kongress.org/2009/slides/compiler_survey_felix_von_leitner.pdf


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


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.


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消