From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30790 invoked by alias); 9 Jan 2008 01:40:54 -0000 Received: (qmail 30781 invoked by uid 22791); 9 Jan 2008 01:40:52 -0000 X-Spam-Check-By: sourceware.org Received: from Unknown (HELO devsrv.otsc.com.cn) (203.187.177.119) by sourceware.org (qpsmtpd/0.31) with ESMTP; Wed, 09 Jan 2008 01:40:35 +0000 Received: from [127.0.0.1] (longhaitao [192.168.0.7]) by devsrv.otsc.com.cn (Postfix) with ESMTP id 1F53D465F64; Wed, 9 Jan 2008 09:36:40 +0800 (CST) Message-ID: <47842617.6010004@otsc.com.cn> Date: Wed, 09 Jan 2008 17:06:00 -0000 From: =?UTF-8?B?6b6Z5rW35rab?= User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 To: John Love-Jensen CC: MSX to GCC Subject: Re: a question about code optimization References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2008-01/txt/msg00081.txt.bz2 John Love-Jensen 写道: > Hi, > >> So my questions is: >> is gcc *smart* enough that she can determine when to do the optimization? > > Yes. > > GCC can do the optimization when the optimizer has sufficient information to > be certain that there are no side effects. > > GCC can not do the optimization when there is a black-box routine involved. > Although there may be some aspects outside of the black-box routine that may > be optimized. > > There is another compiler, LLVM, that does holistic optimizations. If I > recall correct, there is an effort to add (or perhaps merely to investigate > adding) holistic optimizations into GCC. thanks for your patient answer :) another question: --------code begin---------------- vector 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 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? i am sorry because i am not familiar with the optimization term. > > --Eljay >