| View previous topic :: View next topic |
| Author |
Message |
mauro Guest
|
Posted: Tue Nov 01, 2005 11:05 am Post subject: variable definition in a loop |
|
|
Hi all,
following coding:
for (iter = spieler.begin(); iter != spieler.end(); iter++)
{
TSize groesse = Bild.at(x)->Canvas->
TextExtent(String(trikot));
.... // Further steps
}
My question is if I do in this way an overhead in this loop
as I define in every loop a new TSize variable. Or is the
variable destroyed after every loop??
Thanks for any answers
Marius
|
|
| Back to top |
|
 |
liz Guest
|
Posted: Tue Nov 01, 2005 12:45 pm Post subject: Re: variable definition in a loop |
|
|
On 1 Nov 2005 04:05:53 -0700, mauro wrote:
| Quote: | My question is if I do in this way an overhead in this loop
as I define in every loop a new TSize variable. Or is the
variable destroyed after every loop??
|
The optimizier /may/ create a single variable up front.
If you want to, you may also.
If you're concerned about your app's speed, run a profiler to find the
actual bottlenecks.
--
liz
|
|
| Back to top |
|
 |
Duane Hebert Guest
|
Posted: Tue Nov 01, 2005 12:55 pm Post subject: Re: variable definition in a loop |
|
|
"mauro" <Mauro23 (AT) gmx (DOT) de> wrote
| Quote: |
Hi all,
following coding:
for (iter = spieler.begin(); iter != spieler.end(); iter++)
{
TSize groesse = Bild.at(x)->Canvas-
TextExtent(String(trikot));
.... // Further steps
}
My question is if I do in this way an overhead in this loop
as I define in every loop a new TSize variable. Or is the
variable destroyed after every loop??
|
It should be destroyed each iteration. Profile it if
you're not sure. I would tend to do this as the
scope of the variable is at its most limited.
Couple of points though. You're iterating spieler
but you're using Bild.at(x). Where does x come
from? If you know x is in range, you don't need
at(). That's probably adding as much overhead as
the assignment. Also, at() throws if it's not in range
but you don't check that.
You should probably preincrement the iterator as well.
|
|
| Back to top |
|
 |
Chris Uzdavinis (TeamB) Guest
|
Posted: Tue Nov 01, 2005 3:35 pm Post subject: Re: variable definition in a loop |
|
|
"mauro" <Mauro23 (AT) gmx (DOT) de> writes:
| Quote: | Hi all,
following coding:
for (iter = spieler.begin(); iter != spieler.end(); iter++)
{
TSize groesse = Bild.at(x)->Canvas-
TextExtent(String(trikot));
.... // Further steps
}
My question is if I do in this way an overhead in this loop
as I define in every loop a new TSize variable. Or is the
variable destroyed after every loop??
|
The scope of the variable is one iteration of the loop. Thus, before
it performs the next iteration, the local variables will be destroyed,
and then re-created at the beginning of the next iteration. If TSize
is just an integral type, then creating/destroying the variable should
result in no runtime overhead, since they have trivial constructors
and destructors that do nothing.
The overhead you will pay is evaluating the expression on the
right-hand side of the assignment for each loop iteration. Since it
doesn't appear to depend on the loop variable(s), you may be able to
move this code outside the loop, to just calculate it once.
--
Chris (TeamB);
|
|
| Back to top |
|
 |
mauro Guest
|
Posted: Tue Nov 01, 2005 4:25 pm Post subject: Re: variable definition in a loop |
|
|
Thanks to all for the infos...
As an explanation I must say that the variable x is depended
on the loop but I have left the coding out to show my
question in a clearlier way:
for (iter = spieler.begin(); iter != spieler.end(); iter++)
{
x = iter - spieler.begin();
TSize groesse = Bild.at(x)->Canvas->
TextExtent(String(trikot));
...
}
Cheers Marius
| Quote: | The overhead you will pay is evaluating the expression on the
right-hand side of the assignment for each loop iteration. Since it
doesn't appear to depend on the loop variable(s), you may be able to
move this code outside the loop, to just calculate it once.
--
Chris (TeamB);
|
|
|
| Back to top |
|
 |
|