Is
faster thanCode:++x;
? I thought it might be, since ++x can do the increment right then and there, while x++ has to do it later.Code:x++;
Is
faster thanCode:++x;
? I thought it might be, since ++x can do the increment right then and there, while x++ has to do it later.Code:x++;
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
gcc seems to use the same number of instructions for both. I tested with the following 2 programs:
Then I compiled each with: gcc -S <filename>Code:itsme@itsme:~/C$ cat prefix.c int main(void) { int i = 0; int j; j = ++i; } itsme@itsme:~/C$ cat postfix.c int main(void) { int i = 0; int j; j = i++; }
I won't spam the post by actually pasting the entire .s files, but only a few lines changed, and I'll post those:
Code:// prefix.s leal -4(%ebp), %eax incl (%eax) movl -4(%ebp), %eaxThose lines are in the same place as they are in the other file, so there's the same number of instuctions. The rest of the instructions are exactly the same.Code:// postfix.s movl -4(%ebp), %edx leal -4(%ebp), %eax incl (%eax)
Last edited by itsme86; 07-27-2005 at 05:45 PM.
If you understand what you're doing, you're not learning anything.
Hmm, okay, thanks for going to so much trouble.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
You'll find that prefix is faster than postfix for some non-primitive datatypes in C++. (Yes I know this is the C forum.)
I know, that's why I was wondering if prefix was faster for ints too.prefix is faster than postfix for some non-primitive datatypes in C++
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
The only real guarantee is that prefix will be no slower than postfix.
In principle, x++ (for x an int) must be expanded to something like;
while ++x requires no temporary storage of the original value of x. A literal interpretation of that requirement would make postfix slower than prefix operations.Code:int temp = x; x = x+1; /* allow value of temp to be accessible */
However, compilers (or, more accurately, compiler writers) are smarter than that. Most mainstream compilers are able to recognise cases where the original value of x need not be accessible, and therefore avoid storing it's value temporarily. Which is the reason that the example given by itsme86 will usually exhibit no measurable difference in terms of number of instructions or performance.