Original Post
whats the difference between i++ and ++i? i see it used alot in loops. what is the advantage of either method?
Quote:
Original post by Max_Payne
++i is pre-incrementation, while i++ is post-incrementation. They basically mean, increment before the value is evaluated, or increment after the value is evaluated, respectively.
i = 3;x = i++; //x is equal to 3, but i is now 4.j = 3;y = ++i; //both y and j are equal to 4.
Quote:
Original post by tgraupmann
++i; is supposed to give you a small speed improvement.
Quote:
Original post by makeshiftwings
To put it in more simple terms:i = 3;x = i++; //x is equal to 3, but i is now 4.j = 3;y = ++i; //both y and j are equal to 4.
Quote:The reality is that there are a lot of different compilers out there and for many of them there will be a speed difference between the two for non built-in data types.
Original post by Nypyren
If you don't use the intermediate value of 'var++', the compiler removes all the unnecessary code... even in the case of complex stuff like iterators.
This assumes you have a 'good' optimizer.
i = i + 1; // i "is assigned" i "plus" 1i += 1; // i "is incremented by" 1i++; // i "is incremented"++i; // "increment" iSo ++i wins in read(aloud)ability?Quote:
Original post by Nypyren
If you don't use the intermediate value of 'var++', the compiler removes all the unnecessary code... even in the case of complex stuff like iterators.
This assumes you have a 'good' optimizer.
Yes, compilers ARE this smart. Assembly-level dependency graphing is freaking awesome.
Just remember, when you're looking at assembly, if you're using a debug build, you're probably not seeing optimized results.
int main(int argc, char ** argv){ int i = 0; i++; ++i; i += 1; i = i + 1; return 0;} .file "testinc.c" .def ___main; .scl 2; .type 32; .endef .text.globl _main .def _main; .scl 2; .type 32; .endef_main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax movl %eax, -8(%ebp) movl -8(%ebp), %eax call __alloca call ___main movl $0, -4(%ebp) leal -4(%ebp), %eax incl (%eax) leal -4(%ebp), %eax incl (%eax) leal -4(%ebp), %eax incl (%eax) leal -4(%ebp), %eax incl (%eax) movl $0, %eax leave ret leal -4(%ebp), %eax incl (%eax)Quote:
Original post by Inmate2993
*** Source Snippet Removed ***
gcc version 3.2.3 (mingw special 20030504-1).file "testinc.c" .def ___main; .scl 2; .type 32; .endef .text.globl _main .def _main; .scl 2; .type 32; .endef_main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax movl %eax, -8(%ebp) movl -8(%ebp), %eax call __alloca call ___main movl $0, -4(%ebp) leal -4(%ebp), %eax incl (%eax) leal -4(%ebp), %eax incl (%eax) leal -4(%ebp), %eax incl (%eax) leal -4(%ebp), %eax incl (%eax) movl $0, %eax leave ret
I'll draw your attention to the 4 blocks of code that look like so:leal -4(%ebp), %eax incl (%eax)
All 4 types of increment on gcc compilers are equivalent. Any technical interviewer that thought less of me because I used one over the other, I'll tell him to shove that job right up his ass.
Quote:
Original post by Inmate2993
Any technical interviewer that thought less of me because I used one over the other, I'll tell him to shove that job right up his ass.
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more