-1

I've known that primary operator (x++) is different form unary operator (++x) when combine with another operator in a statement.

But I wonder whether those two operator is same when leave them alone on the statement. I mean about compiled code, time to run, ... between:

++x;

and

x++;

Which x is an integer variable.

Daniel Daranas
  • 22,082
  • 9
  • 61
  • 111
Tu Tran
  • 1,927
  • 1
  • 26
  • 47

1 Answers1

4

It is the same in disassembly (not IL) on Windows 8 x64:

                ++x;
0000004a  inc         dword ptr [ebp-40h] 
                x++;
0000004d  inc         dword ptr [ebp-40h] 

As you can see, both statements are an inc instruction. I used this code to try out:

int x = 0;
for (int i = 0; i < 10; i++)
{
    ++x;
    x++;
}
Matthias Meid
  • 12,260
  • 7
  • 43
  • 76