0

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

Can anybody explain me why ++i is faster then i++ when we want to increment integer value I guess it might refer to any programming language.

Community
  • 1
  • 1
danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
  • 3
    Most of the time, it isn't. When it is, it still doesn't matter in about all cases (and when it matters, the ones who ask this kind of question won't be able to resolve it anyway). Really, we've had this crap a thousand times already. Use the search. –  Nov 05 '10 at 22:52
  • I totally agree that this is crap, but this is the question which was asked on interview in Microsoft. – danny.lesnik Nov 05 '10 at 22:54
  • why doesn't that surprise me... http://stackoverflow.com/questions/4101210/i-have-an-interview-with-microsoft-for-a-summer-internship-any-suggestions/4101368#4101368 – Edward Strange Nov 05 '10 at 23:05
  • Guys, beleiveme or not, but I know how to google and this topic only prove what I thought that there is no absolute/ ultimate answer for this question and this is only the reason why I've started this thread. – danny.lesnik Nov 05 '10 at 23:16

3 Answers3

1

It absolutely is not. This is such a common fallacy which somehow keeps on perpetuating.

Note that, when "i" is an object, and an assignment is being made, "i++" will invoke a copy of that object (value before ++), whereas "++i" will not. Modern compilers will not perform a copy of the object if it is not needed.

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
1

Let's do some (pointless) benchmarking to show that it is irrelevant:

The Test Code: (Java)

public class TestIncrement
{
    public static void main(String[] av)
    {
        int x = 0;
        int y = 0;
        long startTime;

        // Postincrement
        startTime = System.currentTimeMillis();
        while ( startTime + 1000 > System.currentTimeMillis())
        {
            x++;
        }

        // Preincrement
        startTime = System.currentTimeMillis();
        while ( startTime + 1000 > System.currentTimeMillis())
        {
            ++y;
        }

        // Output
        System.out.println("Postincrement: " + x);
        System.out.println("Preincrement: " + y);
    }
}   

And the Output:

Postincrement: 16039755
Preincrement: 16069900
Reese Moore
  • 11,524
  • 3
  • 24
  • 32
  • I have also done some testing - partially based on your test. Its further validated in my opinion that the difference between the two can swing either way and at most the difference is only in the range of a few hundred thousand (at that). My version is available here: http://pastebin.com/ZKBeBBN1. – Amndeep7 Jul 25 '12 at 00:13
0

When you have an expression like

k = i++;

k gets the old value of i before incrementation and i gets incremented. This copy creates the performance impact and is not needed with pre-incrementation. But sometimes both are not interchangeable. In this example with

k = ++i;

you will get another value for k.

jdehaan
  • 19,700
  • 6
  • 57
  • 97
  • As others stated this is not really an issue for basic types. If you implement your own operators for custom classes, then it might become a real issue. – jdehaan Nov 05 '10 at 22:56
  • when I'm using incrementation in loop, would it matter when I'm using ++i or i++, or it's relevant only when I'm assigning value. – danny.lesnik Nov 05 '10 at 23:03
  • Sorry, you seem to be saying that `k = ++i` doesn't create a copy? This is dead wrong. Both `k = i++` and `k = ++i` do exactly the same operations, just in a different order. Or am I misunderstanding your answer somehow? – paxdiablo Nov 05 '10 at 23:06
  • I have to -1 on this answer. The question is specifically about integer, not about some complex type with a lot of copy expense. Any possible expense of post++ over ++pre is going to be VERY implementation and architecture dependent. There's not even any reason to believe a copy is made unless it's being assigned and there's a LOT of room for compiler optimizations here. In fact, there's no guarantee that ++pre isn't the more expensive one. The fact that MS would ask a pre-micro-optimization Q like this speaks volumes. – Edward Strange Nov 05 '10 at 23:10