From: Christoph Bartoschek <bartoschek@gmx.de>
To: gcc-help@gcc.gnu.org
Subject: Re: a question about code optimization
Date: Sat, 22 Dec 2007 10:20:00 -0000 [thread overview]
Message-ID: <200712221121.22411.bartoschek@gmx.de> (raw)
In-Reply-To: <476CA397.2010505@otsc.com.cn>
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
next prev parent reply other threads:[~2007-12-22 10:20 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-22 5:41 龙海涛
2007-12-22 10:20 ` Christoph Bartoschek [this message]
2007-12-25 3:00 ` 龙海涛
[not found] <C3A0E1D1.29B03%eljay@adobe.com>
2008-01-04 14:38 ` 龙海涛
2008-01-05 14:25 ` John Love-Jensen
2008-01-09 17:06 ` 龙海涛
2008-01-09 23:03 ` 龙海涛
2008-01-10 19:51 ` John Love-Jensen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200712221121.22411.bartoschek@gmx.de \
--to=bartoschek@gmx.de \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).