Results 1 to 5 of 5
Code:
for ( int i = 0; i < x.size(); i++ ) {
...
}
In the code above, the condition of this for loop is tested by a function ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 04-29-2003 #1Linux Engineer
- Join Date
- Nov 2002
- Location
- Queens, NY
- Posts
- 1,319
compile optimization
In the code above, the condition of this for loop is tested by a function call. If x.size() returns a relatively small number, it won't make much difference in speed. However, what happens if x.size() returns 100,000,000? This results in x.size() being called 100,000,001 times. A better solution would can be demonstrated in the follwing code.Code:for ( int i = 0; i < x.size(); i++ ) { ... }
As you can see, all I did was store the value from x.size() to variable stop. I've read that some compilers will optimize the code that I used in the first segment. I'm assuming that optimization is performed on the loop by manipulating the loop condition as I have done in the second segment. Does anyone know anything about this? Does gcc/g++ have these options?Code:int stop = x.size(); for ( int i = 0; i < stop; i++ ) { ... }
The best things in life are free.
- 04-29-2003 #2Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
It cannot do this normally, since it doesn't now what will happen if x.size isn't called as many times as it's supposed to. Maybe it changes global variables or returns different values each time?
However, in GCC, these two will be the same:
Code:int size(void) { return(something good); } ... stop = size(); for(i = 0; i < stop; i++) { .... }__attribute__ ((pure)) specifies gives size about the same status as an arithmetic operator, in that the compiler knows that it doesn't modify anything, and it return value depends only on its parameters and on global variables. To be even more strict, specify __attribute__ ((const)), which speicifies that the function doesn't even depend on global values, and doesn't examine any pointers that it is passed.Code:int __attribute__ ((pure)) size(void) { return(something good); } ... for(i = 0; i < size(); i++) { .... }
The strange is that the GCC texinfo manual clearly specifies this, but when I try it and look at the assembler output, it doesn't optimize it that way. I don't know what's wrong; maybe I've just missed some little thing.
Neither do I know if it works in C++.
- 04-30-2003 #3Linux Engineer
- Join Date
- Nov 2002
- Location
- Queens, NY
- Posts
- 1,319
thanks
Dolda,
It looks like I'll have to do some more reading up on this topic. Just out of curiosity, where did you get this info from?The best things in life are free.
- 04-30-2003 #4Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
In GCC's texinfo documention, under "C Extensions" and "Function Attributes".
- 05-01-2003 #5Linux Engineer
- Join Date
- Nov 2002
- Location
- Queens, NY
- Posts
- 1,319
Dolda,
Not that I'm ungrateful but is there anyone else besides Dolda who answers my questions? I gotta say majority(90%) are all answered by Dolda.The best things in life are free.


Reply With Quote
