BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

variable definition in a loop

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
mauro
Guest





PostPosted: Tue Nov 01, 2005 11:05 am    Post subject: variable definition in a loop Reply with 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??

Thanks for any answers
Marius
Back to top
liz
Guest





PostPosted: Tue Nov 01, 2005 12:45 pm    Post subject: Re: variable definition in a loop Reply with quote



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





PostPosted: Tue Nov 01, 2005 12:55 pm    Post subject: Re: variable definition in a loop Reply with quote




"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





PostPosted: Tue Nov 01, 2005 3:35 pm    Post subject: Re: variable definition in a loop Reply with quote

"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





PostPosted: Tue Nov 01, 2005 4:25 pm    Post subject: Re: variable definition in a loop Reply with quote


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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.