public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* stack variables and loops
@ 2008-03-19  5:36 Wesley Smith
  2008-03-19 13:18 ` Diego Novillo
  0 siblings, 1 reply; 3+ messages in thread
From: Wesley Smith @ 2008-03-19  5:36 UTC (permalink / raw)
  To: MSX to GCC

Hi,
I'm sure this is a simple question for all of the hardcore compiler
people on this list, but I'm about how compilers (and specifically
GCC) handle stack variables allocated exterior to versus interior to a
for loop.  For example:


//assume Vec3 is a triplet of floats and Quat is a quaternion (quadruplet)

/////example 1

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.
Obviously for these examples to be "equivalent", v_rand would have to
only be referenced inside the loop and nowhere else in the latter
example.

Apologies if this if this post is inappropriate or too speculative.
I'm just trying to get a sense of how the underlying compiler
technology works in these situations.

best,
wes

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: stack variables and loops
  2008-03-19  5:36 stack variables and loops Wesley Smith
@ 2008-03-19 13:18 ` Diego Novillo
  2008-03-19 15:24   ` Wesley Smith
  0 siblings, 1 reply; 3+ messages in thread
From: Diego Novillo @ 2008-03-19 13:18 UTC (permalink / raw)
  To: Wesley Smith; +Cc: MSX to GCC

2008/3/19 Wesley Smith <wesley.hoke@gmail.com>:

>  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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: stack variables and loops
  2008-03-19 13:18 ` Diego Novillo
@ 2008-03-19 15:24   ` Wesley Smith
  0 siblings, 0 replies; 3+ messages in thread
From: Wesley Smith @ 2008-03-19 15:24 UTC (permalink / raw)
  To: Diego Novillo; +Cc: MSX to GCC

Hey wow!
That's very informative.
thanks,
wes

On Wed, Mar 19, 2008 at 6:17 AM, Diego Novillo <dnovillo@google.com> wrote:
> 2008/3/19 Wesley Smith <wesley.hoke@gmail.com>:
>
>
>  >  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.
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-03-19 15:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-19  5:36 stack variables and loops Wesley Smith
2008-03-19 13:18 ` Diego Novillo
2008-03-19 15:24   ` Wesley Smith

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).