Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

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
like image 925
abhi4eternity Avatar asked Aug 24 '10 14:08

abhi4eternity


People also ask

What is i += 1 in C?

i+=i means the i now adds its current value to its self so let's say i equals 10 using this += expression the value of i will now equal 20 because you just added 10 to its self. i+=1 does the same as i=i+1 there both incrementing the current value of i by 1.

Which one is faster i ++ or i 1 explain?

As i++ does automatic typecasting and uses a compiler instruction which internally uses iadd instruction, i=i+1 is faster than i++.

What is the difference between ++ i and ++ i?

In the prefix version (i.e., ++i), the value of i is incremented, and the value of the expression is the new value of i. So basically it first increments then assigns a value to the expression. In the postfix version (i.e., i++), the value of i is incremented, but the value of the expression is the original value of i.

Why is ++ faster than i ++?

I've heard this question come up a few times in C++ programming circles, “Why is ++i faster/better than i++?” The short answer is: i++ has to make a copy of the object and ++i does not.


3 Answers

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.

like image 148
fredoverflow Avatar answered Oct 04 '22 11:10

fredoverflow


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.

like image 26
Guffa Avatar answered Oct 04 '22 12:10

Guffa


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.

like image 29
polygenelubricants Avatar answered Oct 04 '22 13:10

polygenelubricants