a question about code optimization

龙海涛 longhaitao@otsc.com.cn
Wed Jan 9 23:03:00 GMT 2008


> another question:
> 
> --------code begin----------------
> vector<int> a;
> ...
> for(int i = 0; i < a.size(); i++)
> {
>     /*do something*/
> }
> -------code end---------------------
> 
> i asked that if this can be optimized to this:
> ----------code begin--------------
> vector<int> a;
> ...
> int size = a.size();
> for(int i = 0; i < size; i++)
> {
>     /*do something*/
> }
> ----------code end-----------------
> 
> last time we discussed one situation may influence the optimization:
> 
> a) in function size() we may change some global variables.
> 
> there is another situation:
> 
> b) in the /*do something*/ block, we may use a.push_back(...) or some 
> other functions
> that can change the size of a. if this happen, of course we can not do 
> the optimization.
> 
> so can gcc know what functions may change the size of a?
> is gcc smart enough to optimize the code if those functions are not used?
well, i think the possible implementations are:

a) we tell the gcc what function may change the size of a vector
b) in the internal of gcc, gcc can find them out

which one is more reasonble? or none of them?

-- Kevin Long



More information about the Gcc-help mailing list