to prototype
yes,,I am clear about what you talk about.i just read your answer quickly then give an answer.what you said is right
of course ,In a complex env.we should move out/in them.
but the operation that waste time mostly must be the object's construct/deconstruct.but not move in/out.I think it is more important.
please pay attention to my wording. all i said in my last post was 'in principle'. in practice, the temporary object 'tmp' could be optimized away. as you said, the temporary object may be just in a register. but that doesn't necessarily mean the overhead of 'i++' is the same as '++i'. it is depending on the context. stand-alone 'i++' is nearly guaranteed to be optimized to be the same as '++i'. in some other simple cases, 'i++' can also be the same as '++i'. but this is not guaranteed for all.
consider a expressin like:
func1() + func2( i++ ); // func() may be a very complex function, a register can not hold 'i''s value all the way till the end of the function call.
this could be executed as:
func1() + func2( i ); // note that at the end of this statement, 'i's value is not in the register.
i = i + 1; // computer has to read 'i's value into register and then does the addition.
compare it with the '++i' case:
func1() + func2(++i);
it will be executed like:
i = i + 1; // 'i's value is still in a register at the end of this statement.
func1() + func2( i );
so computer has to read 'i' one more time in 'i++' case than in '++i' case.
(am i missing anything here?)
oh, a note for my last post:
'int tmp = i; i=i+1; /* then use 'tmp' as 'i' in your expression.*/'
precisely, the above sentence should be written as:
'int tmp = i; /* then use 'tmp' as 'i' in your expression.*/ i=i+1;
notice 'i' is only updated after the evaluation of expression finishes. whether 'i' is still kept in a register after the evaluation is depending how complex the expression is.
'++i' is equivalent to: 'i=i+1; /* then use 'i' as the 'i' in your expression. */'
'i++' is equivalent to: 'int tmp = i; i=i+1; /* then use 'tmp' as 'i' in your expression. */'
so '++i' is a bit more efficient than 'i++'. however, if you just write a stand-along 'i++' or '++i', the temporary object 'tmp' is unnecessary, so any compiler should be able to completely optimize that way, making 'i++' is equally efficient as '++i'.