Re: Declaring local variables inside loop
- From: Jacob <jacob@xxxxxxxxx>
- Date: Wed, 22 Feb 2006 14:07:34 +0100
Hendrik Maryns wrote:
I do some looping like this:
FunctionInputTuple tupleWithSecond;
while (tupleStates.contains(first)) {
tupleWithSecond = FunctionInputTuple.getInstance(
tuple.getSymbol(), tupleStates);
// do some computation, replace ?first? in the list
equivalent &= equivalence.equivalent(firstValue,
secondValue);
}
Now my question is: does it matter that I declare tupleWithSecond
outside the loop? I have the impression that if I leave the first line
out and instead add ?FunctionInputTuple? before the third line, that a
new pointer space is created on the stack for each loop. Or is the
compiler smart enough to see this and optimise it away?
In general you should limit the scope of names, so
declaring the variable inside the loop is preferable.
I don't know the technical details and the two cases
*may* in principle differ performance wise (I don't
think they do), but this whould be what is commonly
known as "premature optimization" and should be avoided
anyway.
.
- Follow-Ups:
- Re: Declaring local variables inside loop
- From: bugbear
- Re: Declaring local variables inside loop
- From: Robert Klemme
- Re: Declaring local variables inside loop
- References:
- Declaring local variables inside loop
- From: Hendrik Maryns
- Declaring local variables inside loop
- Prev by Date: Re: Declaring local variables inside loop
- Next by Date: Re: The best data structure
- Previous by thread: Re: Declaring local variables inside loop
- Next by thread: Re: Declaring local variables inside loop
- Index(es):
Relevant Pages
|