++m and m++ are different in terms of run time.

Aug 4, 2008 at 2:12am
How could ++m and m++ are different in terms of run time?
and could even have a long code like this????
x = ++++++++x; but cannot make like this x++++++++;
Aug 4, 2008 at 2:22am
++m: This will first increase the value of 'm' by one and then use it in the expression.
m++: This will first use the current value of 'm' and then increase it by one.

Consider the following example:
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;
int main(){
   int m=0;
   cout << m++ << '\n';
   cout << ++m << '\n';
   return 0;
}


If you run this you will have as results:
0
2

You can understand from that that the first cout takes as value of 'm' the original one (0) and then the program increases m. In the second cout the program first increases the value of 'm' and then sends it to the console.
Aug 4, 2008 at 7:35pm
How could ++m and m++ are different in terms of run time?


m++ will copy the value first, then increment it.
++m will simply increment it. So it saves you a copy operation

Time Saved = Sweet FA
Aug 13, 2008 at 10:48am
thanks for answering my question guys..
ive learn alot of it..
and in addition to wat ive learn...

if you have m++
you use STACK which has PUSH and POP
in machine language it looks like this:

push m
mov eax, m
inc m
pop m

if you have m++++ (2 seats)

push m
mov eax, m
inc m

push m
mov eax, m
inc m

pop m
pop m

same value.... everytime you pop.

if you use ++++m (two seats)
it directly incremented

inc x
inc x

no STACKS had been used...
Topic archived. No new replies allowed.