From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31727 invoked by alias); 19 Mar 2008 13:18:18 -0000 Received: (qmail 31715 invoked by uid 22791); 19 Mar 2008 13:18:17 -0000 X-Spam-Check-By: sourceware.org Received: from smtp-out.google.com (HELO smtp-out.google.com) (216.239.45.13) by sourceware.org (qpsmtpd/0.31) with ESMTP; Wed, 19 Mar 2008 13:17:54 +0000 Received: from zps77.corp.google.com (zps77.corp.google.com [172.25.146.77]) by smtp-out.google.com with ESMTP id m2JDHoDc018129 for ; Wed, 19 Mar 2008 06:17:50 -0700 Received: from fg-out-1718.google.com (fge13.prod.google.com [10.86.5.13]) by zps77.corp.google.com with ESMTP id m2JDHEQQ024770 for ; Wed, 19 Mar 2008 06:17:49 -0700 Received: by fg-out-1718.google.com with SMTP id 13so408047fge.11 for ; Wed, 19 Mar 2008 06:17:49 -0700 (PDT) Received: by 10.86.31.18 with SMTP id e18mr130506fge.35.1205932669262; Wed, 19 Mar 2008 06:17:49 -0700 (PDT) Received: by 10.86.1.11 with HTTP; Wed, 19 Mar 2008 06:17:49 -0700 (PDT) Message-ID: Date: Wed, 19 Mar 2008 13:18:00 -0000 From: "Diego Novillo" To: "Wesley Smith" Subject: Re: stack variables and loops Cc: "MSX to GCC" In-Reply-To: <1079b050803182236ud33b5bfqe4148ff53dcc2681@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <1079b050803182236ud33b5bfqe4148ff53dcc2681@mail.gmail.com> 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-03/txt/msg00174.txt.bz2 2008/3/19 Wesley Smith : > Vec3 *v = vectors; > for(int i=0; i < 100; i++) { > Vec3 v_rand; > v_rand.random(); //make a random vector; > > *v++ += v_rand; > } > > > /////example 2 > > Vec3 *v = vectors; > Vec3 v_rand; > for(int i=0; i < 100; i++) { > v_rand.random(); //make a random vector; > > *v++ += v_rand; > } > > > Clearly there are scope differences between the 2 examples, but in > terms of performance, how do compilers handle these situations. The first version needs to have a ctor and dtor call invoked on every iteration of the loop. Depending on what they do that will slow the code down. If you insert a printf() call in Vec3's constructor and destructor you'll see the difference. With GCC 4+ you can also use -fdump-tree-gimple to get a C-like representation of your program. Diego.