a question about code optimization

龙海涛 longhaitao@otsc.com.cn
Fri Jan 4 14:38:00 GMT 2008

John Love-Jensen дµÀ:
> Hi,
> In this particular example, the size() method is inline, so the compiler
> injects it locally into the code, and hence the compiler is aware if size()
> changes some global variable or not.
> If size() were non-inline, it would be a "black box" to the optimizer, so in
> that case your analysis is correct.
> --Eljay

yes, size() function is inline, but consider this situation: we can call
some non-inline functions in inline function,i.e,

void foo_non_inline();

inline int foo_inline() {

obviously we are not sure that the global variabls are changed or not in foo_non_inline();

So my questions is:
is gcc *smart* enough that she can determine when to do the optimization?

> On 12/21/07 11:41 PM, "Áúº£ÌÎ" <longhaitao@otsc.com.cn> wrote:
>> ---------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?

More information about the Gcc-help mailing list