a question about code optimization

Christoph Bartoschek bartoschek@gmx.de
Sat Dec 22 10:20:00 GMT 2007


Am Samstag 22 Dezember 2007 schrieb 龙海涛:
> ---------code-----------
> vector<int> a;
> /*do somthing*/
> for(int i = 0; i<a.size(); i++) {
> 	/* do something*/
> }
> --------code end--------
>
> my question is:
> is it possible that the compiler will transform the code to this:
>
> int __tmp = a.size();
> for(int i = 0; i < __tmp; i++) {
> 	/*do something*/
> }
>
> i think it is impoossible,because in size() function, the programmer
> can change some global variables, so the compiler could not do that.
>
> if gcc can, could you tell me how?
>
> i am sorry in advance because i am not familiar with the code
> optimization. so i do not know how to search the gcc manual and gcc
> internal manual to find answer.could anyone tell me how to find this in
> the manual?or at least give some keywords?

Here the situation is quite simple. Because a is a template the 
vector<int>::size() method is probably included in the header files. This 
means that the compiler can examine or inline the code of the method. If the 
compiler additionally determines that in /*do something*/ the size is not 
changed, then the optimization is quite straigthforward.

Christoph



More information about the Gcc-help mailing list