Android Question Where to Dim for usage in loop

galimpic

Member
Licensed User
Longtime User
If i need to use a temporary variable inside a loop, is it better (performance-wise) to declare it before the loop, or is it all the same if I do it inside the loop? For example:

B4X:
Dim x as Int
For i = 0 to 100
  x = i * i
  ...
Next

vs.

B4X:
For i = 0 to 100
  Dim x as Int = i * i
  ...
Next
 

galimpic

Member
Licensed User
Longtime User
I am talking about situations where it doesn't matter, not where it makes a difference, of course. That's why I said "temporary variable". The reason I'm asking is because I like declaring and initializing variables on the same line, but if it makes the code work harder (releasing and reclaiming memory, creating many instances of variables to be garbage collected etc.), I will switch to doing it before the loop. This is, of course, a trivial example, but there are huge loops with many variables and it may make a difference. I really don't know what Java code is executed on "Dim", whether if it is just straight declaration or not.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Erel is, of course, right. Over-optimizing can be counter-productive. But just for educational purposes, I think calling "Dim" will translate into executing the object's default no-argument constructor.
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
Dim before.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
before unless you are using objects/views.

and only if it can gain something speedwise (in math to prevent the use of the slower stuff like sqr/power)
 
Upvote 0

galimpic

Member
Licensed User
Longtime User
@JonPM Actually, if a loop executes 1000 times and it creates a new variable every time because of a Dim inside a loop, with a lot of "dead" variables to be destroyed by garbage collector later - it does bother me and I want to know. We are not dealing with government secrets here. I understand that it may not be critical, and it is a good advice to pay more attention to other aspects of my program, but those points are not answering the question. I will be more than happy to engage in that sort of discussion later, when the topic of this thread has been resolved. It stands to reason that "Dim before" is the best practice, I was just asking if someone is sure what happens behind the scenes.
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
Well, I'm not sure what happens behind the scenes, but I believe that
it creates a new variable every time because of a Dim inside a loop, with a lot of "dead" variables to be destroyed by garbage collector later
is exactly what happens.

Generally, it just seems wrong to Dim inside the loop, if the machine was a person, I think it would be a lot more logical to say:
"Prepare your tools, you'll be working an 8 hour shift", rather than "Go get a screwdriver, use it and put it back every 10 minutes".
 
Upvote 0

galimpic

Member
Licensed User
Longtime User
What this topic really boils down to is: when we Dim the same variable name for the second time within the same scope, does it create a brand new variable masking the old one, or is it just ignoring the Dim and resetting (i.e. reusing) the old one? It is possible to implement it both ways in the process of converting b4a code to Java. We can only guess it is one or the other, unless we get the answer from Erel (or, theoretically, reverse compile the Java code)
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
Although it seems logical to Dim before, it seems that regarding Java (and B4A is Java) most Stack Overflow users argue otherwise:
The scope of local variables should always be the smallest possible.
In your example I presume str is not used outside of the while loop, otherwise you would not be asking the question, because declaring it inside the while loop would not be an option, since it would not compile.

So, since str is not used outside the loop, the smallest possible scope for str is within the while loop.

So, the answer is emphatically that str absolutely ought to be declared within the while loop. No ifs, no ands, no buts.

The only case where this rule might be violated is if for some reason it is of vital importance that every clock cycle must be squeezed out of the code, in which case you might want to consider instantiating something in an outer scope and reusing it instead of re-instantiating it on every iteration of an inner scope. However, this does not apply to your example, due to the immutability of strings in java: a new instance of str will always be created in the beginning of your loop and it will have to be thrown away at the end of it, so there is no possibility to optimize there.

EDIT: (this is something that I wrote in a comment below, but I think it is worth making part of the answer.)

In any case, the right way to do things is to write all your code properly, establish a performance requirement for your product, measure your final product against this requirement, and if it does not satisfy it, then go optimize things. And what usually ends up happening is that you find ways to provide some nice and formal algorithmic optimizations in just a couple of places which make our program meet its performance requirements instead of having to go all over your entire code base and tweak and hack things in order to squeeze clock cycles here and there.

http://stackoverflow.com/questions/8803674/declaring-variables-inside-or-outside-of-a-loop
 
Upvote 0

galimpic

Member
Licensed User
Longtime User
@wonder this applies to Java, where a variable declared inside a loop will be a block variable and therefore not visible outside of the loop. In b4a, this variable will be available from the declaration to the end of the Sub, so this reasoning does not apply.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
in assembler it wouldn't matter since you just point to a memory location and you don't really declare strings or ints since it's all a single or sets of bytes.
 
Upvote 0

galimpic

Member
Licensed User
Longtime User
Actually, I did start with assembler in the 80's, along with (then necessary) need to squeeze all data in a few kilobytes and processing power in a few megahertz. I guess that's why I'm not comfortable with wasteful practices of today's programming.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
the benefit is that compilers rewrite you source if possible to make it perform better (that's why current compilers are slower than tasm or older compilers like TP/BP, WatcomC ...)
 
Upvote 0

galimpic

Member
Licensed User
Longtime User
Mystery solved. BOTH examples from the first post compile as:

B4X:
int i = 0;
int j = 0;
   
i = 0;
for (j = 0; j <= 100; j = 0 + j + 1) {
  i = j * j;
}

Therefore, it makes no difference whether the variable (primitive, not object) is declared before or inside the loop, the compiler will move the declaration before. Case closed.
 
Upvote 0
Top