Try it and time it!!
Place yer bets.
Lets say you had a program that consists totally of the statement:
a++;
over and over again.
Then another program that consists of the same number of statements but it's:
a = a + 1;
Now the two binaries generated will be the same, but which will take less time to compile?
I believe a++ is just translated to a = a + 1; in which case it'd have that tiny bit of overhead, but wait, a++; takes up less space, so it can load the file off the hard drive faster!
No practical value to this, but it's kinda curious.
I think the compiler sees them as the same thing initially. Take the contents of the register referenced by a and add 1 to it, place that new value back into that register. So same compile time.
It would depend on your preprocessor and compiler.
If you are using a compiler which doesn't support it, then the preprocessor will know and change it.
However if you are using a newer one which does support it, but the preprocessor doesn't know the compiler supports it, you get the same time.
Third case, the preprocessor knows that the compiler understands i++ and leaves it alone, then the compiler compiles the binary code.
Remember whenever you are compiling code a preprocessor goes through and strips out comments and does all sorts of fancy wizadry.
CP
I believe the compiler will create different codes for the two.
i++ will create assembly code something like INC A, and i = i +1 will generate ADD A,1. Syntax will vary by target processor, but the idea is the same.
[quote author=cloverm link=board=9;threadid=9944;start=0#msg90123 date=1099273796]
I believe the compiler will create different codes for the two.
i++ will create assembly code something like INC A, and i = i +1 will generate ADD A,1. Syntax will vary by target processor, but the idea is the same.
[/quote]
Ah but that all depends on whether you're using a RISC or CISC processor. On a RISC processor it's almost guaranteed to compile to the same thing (otherwise it would be a very poor RISC processor). Anyway if I may chip in my theory (assuming RISC), I would think that the lexical scanner would scan through the file (negligible amount of time, but if you really want to break it down, i++ would probably happen faster due to fewer lexical tokens to process), and then the compiler would construct a parse tree for the two of them that probably looks identical in each case, so from there on in it would probably be the same.
[quote author=Radar link=board=9;threadid=9944;start=0#msg90121 date=1099271179]
I think the compiler sees them as the same thing initially. Take the contents of the register referenced by a and add 1 to it, place that new value back into that register. So same compile time.
[/quote]
Ah but it takes longer to see i = i +1; because it's a couple of bytes longer.
The INC versus ADD thing is interesting, and RISC versus CISC. In CISC, it seems INC would be faster than ADD (or else what's the point?), so shouldn't a good compiler optimize i = i + 1; into an INC statement?
Oh also if the compiler isn't optimizing well ++i versus i++ (i++ has to store the old i, then increment, and return old i)
[quote author=cloverm link=board=9;threadid=9944;start=0#msg90123 date=1099273796]
I believe the compiler will create different codes for the two.
i++ will create assembly code something like INC A, and i = i +1 will generate ADD A,1. Syntax will vary by target processor, but the idea is the same.
[/quote]That sounds right to me. I'm with you on this one.
[quote author=cloverm link=board=9;threadid=9944;start=0#msg90123 date=1099273796]
I believe the compiler will create different codes for the two.
i++ will create assembly code something like INC A, and i = i +1 will generate ADD A,1. Syntax will vary by target processor, but the idea is the same.
[/quote]
I'm quite sure the compiler will only do that if all optimization is turned off - otherwise it would be logical for the compiler to do the smart thing.
Bookmarks