Declaring variable in loop inefficient?

I think I read somewhere on these forums that it more efficient to declare a variable outside of a loop. Is this true?

I am hoping somebody on these forums have already tried profiling this because I can remember reading something about it. It might be the opposite that it is more efficient to declare within a loop but meeting the conditions that the loop is with a bunch of other code outside and inside the for loop. Which would be more efficient?

dim i as integer
for b as integer = 0 to 10
i = GetInt
next

Or:

for b as integer = 0 to 10
dim i as integer = GetInt
next

Or:

if true then
dim i as integer
for b as integer = 0 to 10
i = GetInt
next
end if

One just declares outside the for loop, one declares inside the for loop and one declares outside a for loop but inside an if statement to scope the dim statement.

I would presume this would be most efficient but I am not sure? I am also unsure on how little difference it makes in terms of speed. Hopefully nothing to worry about but I am just checking.

for b as integer = 0 to 10
dim i as integer = GetInt
next

Thanks

I would declare outside the loop.
So this is the fastest:

dim i as integer for b as integer = 0 to 10 i = GetInt next

[quote=100151:@Christoph De Vocht]I would declare outside the loop.
So this is the fastest:

dim i as integer for b as integer = 0 to 10 i = GetInt next[/quote]
Thanks

The question becomes, How many times are you doing it? The tradeoff is between speed and encapsulation. If the speed difference is slight, then encapsulation wins. If you find that in real world tests, the speed degredation becomes pronounced / noticeable, then move the dim outside the loop (and use the IF trick to encapsulate it, if desired). Bottom line, though:

Avoid Premature Optimization

[quote=100158:@Tim Hare]The question becomes, How many times are you doing it? The tradeoff is between speed and encapsulation. If the speed difference is slight, then encapsulation wins. If you find that in real world tests, the speed degredation becomes pronounced / noticeable, then move the dim outside the loop (and use the IF trick to encapsulate it, if desired). Bottom line, though:

Avoid Premature Optimization[/quote]
I wasn’t sure if it was very much a premature optimization. Thanks

Unless you know from prior experience that something makes a big difference, then if you haven’t run the code and detected a problem, either through profiling or by feel, then it’s premature. Focus on writing correct code - it does what you want - and make it faster/more efficient when you know there’s a need. Even the “biggies” can be premature if it turns out that you’re only doing it once. In that case, doing it the “fast way” doesn’t really help much, so write clean readable code that will be easy to maintain and don’t worry about optimization until later.

Any time you start with “is this faster than that” instead of “is this code more correct/readable” you’re optimizing too soon

Now that doesn’t mean you should write bad code thats clearly understandable first time off - but start with correct readable code THEN profile it and you may be surprised where things need to be improved & not

Not to disrespect the answers but I don’t the the OP was answered. At least not how I read it.

I agree with not optimizing early, but I think the question was “is moving the declaration out of the loop an optimization?” which is probably only answerable by a very few people who understand how the compiler works…

Can anyone answer the question with that particular perspective?

my 2cents :slight_smile:

My 3 commandments (as stated previously)

  • Thou shall write code that is Readable by you and your peers now and into the future
  • Thou shall write code that is easily Maintainable by you and your peers now and into the future
  • Thou shall write code that is of course Executable as efficiently as possible without violating commandments 1 and 2

[quote=100215:@Steve Upton]Not to disrespect the answers but I don’t the the OP was answered. At least not how I read it.

I agree with not optimizing early, but I think the question was “is moving the declaration out of the loop an optimization?” which is probably only answerable by a very few people who understand how the compiler works…

Can anyone answer the question with that particular perspective?[/quote]
Dim is an executable statement. Therefore if you can execute it once vs multiple times, it is an optimization. Whether it’s enough of an optimization to be worth while is debatable.

Put variables in the narrowest scope required
If that means inside the loop then thats where they belong
The hit to declare it & do that over & over is likely minimal compared to declaring them all at the top & then using it when it should not be considered valid any longer
So make that true by putting things in the correct scope - and worry about “gee we do this a billion times & its slow to do that” - when you find it IS a performance issue

Thanks people