J
J. Campbell
Hi everyone. I'm sure that this is common knowledge for many/most
here. However, it was rather illuminating for me (someone learning
c++) and I thought it might be helpful for someone else. I'm sure I'll
get scolded for an OT post, but I think issues of this type should be
of interest to programmers until they get a handle on them.
I was reading some old threads about the relative advantages of using
++i vs i++ (I won't rehash the many nice discussions that predate this
post) and I decided to do some tests. I'm using the dev-cpp IDE (v
4.9.8.0) and the included compiler on a wintel machine. These results
were obtained using integers, and not the overloaded operators.
What I did was create an empty loop of the following type to determine
the time required to count up to or down 2,000,000,000 units.
e.g.
..
int temp1, temp2;
int i;
temp1 = clock();
for(i = 0; i < 2000000000; i++);
temp2 = clock();
cout << temp2-temp1;
..
..
I found several things.
1) Using different optimization settings can cause dramatically
varying results.
2) The "best" optimization setting does not necessarily produce the
fastest code. In fact, in these tests, using the "third worst"
optimization setting produced the best result. (that is, "minor
optimizations"="On" and "further optimizations"-->"optimize" = "On"
3) using the "++" or "—" operator is not faster than using "i+=b"
4) When a variable is used as the incrementor, or in the comparator, a
speed increase occurs compared to using a literal constant.
5) However, a special case which gives a speed boost (only with the
mentioned optimization settings) is when the comparitor evaluates to
"i!=0" (using the literal constant) or simply "i".
So what am I talking about? Here are some times I got.
for(i=0; i!=2000000000; ++i); //2.7 secs opt 3
for(i=0; i!=2000000000; i++); //2.7 secs opt 3
for(i=0; i!=2000000000; i+=1); //2.7 secs opt 3
int b = 1; for(i=0; i!=2000000000; i+=b); //2.0 secs opt 2
int z=2000000000; int b = 1; for(i=0; i!=b; i+=1); //2.0 secs opt 4
int z=-2000000000; int b=1; for(i=z; i!=0; i+=b); //1.6 secs opt 2
int z=-2000000000; int b=1; int y=0; for(i=z; i!=y; i+=b) //2.0s opt=2
int z=-2000000000; int b=1; for(i=z; i!=0; ++i) //2.0 secs opt 3
So...I guess my main conclusion is...that using a variable instead of
using a literal constant in the "b=1; i+=b;" form can be faster than
"++1". And...that the labels on the optimization settings should be
taken with a grain of salt. BTW...the times I show were the best
times after trying all 5 optimization settings that the compiler
offered for each increment type.
I found no speed difference in the increment vs decrement
times...other than the incidental one that occurs whey you count down
to 0...then a substantial speed gain occurs because this uses the
special !=0 case.
Again...sorry if this is taken as OT. The comp.compiler forum
moderator refers specific compiler inquiries to the appropriate
comp.lang forum...kinda a circular problem...
here. However, it was rather illuminating for me (someone learning
c++) and I thought it might be helpful for someone else. I'm sure I'll
get scolded for an OT post, but I think issues of this type should be
of interest to programmers until they get a handle on them.
I was reading some old threads about the relative advantages of using
++i vs i++ (I won't rehash the many nice discussions that predate this
post) and I decided to do some tests. I'm using the dev-cpp IDE (v
4.9.8.0) and the included compiler on a wintel machine. These results
were obtained using integers, and not the overloaded operators.
What I did was create an empty loop of the following type to determine
the time required to count up to or down 2,000,000,000 units.
e.g.
..
int temp1, temp2;
int i;
temp1 = clock();
for(i = 0; i < 2000000000; i++);
temp2 = clock();
cout << temp2-temp1;
..
..
I found several things.
1) Using different optimization settings can cause dramatically
varying results.
2) The "best" optimization setting does not necessarily produce the
fastest code. In fact, in these tests, using the "third worst"
optimization setting produced the best result. (that is, "minor
optimizations"="On" and "further optimizations"-->"optimize" = "On"
3) using the "++" or "—" operator is not faster than using "i+=b"
4) When a variable is used as the incrementor, or in the comparator, a
speed increase occurs compared to using a literal constant.
5) However, a special case which gives a speed boost (only with the
mentioned optimization settings) is when the comparitor evaluates to
"i!=0" (using the literal constant) or simply "i".
So what am I talking about? Here are some times I got.
for(i=0; i!=2000000000; ++i); //2.7 secs opt 3
for(i=0; i!=2000000000; i++); //2.7 secs opt 3
for(i=0; i!=2000000000; i+=1); //2.7 secs opt 3
int b = 1; for(i=0; i!=2000000000; i+=b); //2.0 secs opt 2
int z=2000000000; int b = 1; for(i=0; i!=b; i+=1); //2.0 secs opt 4
int z=-2000000000; int b=1; for(i=z; i!=0; i+=b); //1.6 secs opt 2
int z=-2000000000; int b=1; int y=0; for(i=z; i!=y; i+=b) //2.0s opt=2
int z=-2000000000; int b=1; for(i=z; i!=0; ++i) //2.0 secs opt 3
So...I guess my main conclusion is...that using a variable instead of
using a literal constant in the "b=1; i+=b;" form can be faster than
"++1". And...that the labels on the optimization settings should be
taken with a grain of salt. BTW...the times I show were the best
times after trying all 5 optimization settings that the compiler
offered for each increment type.
I found no speed difference in the increment vs decrement
times...other than the incidental one that occurs whey you count down
to 0...then a substantial speed gain occurs because this uses the
special !=0 case.
Again...sorry if this is taken as OT. The comp.compiler forum
moderator refers specific compiler inquiries to the appropriate
comp.lang forum...kinda a circular problem...