Optimisation of the For/Next loops (Tutorial Request)

Cableguy

Expert
Licensed User
Longtime User
Hi guys,

I've been digging into customview creation, and found myself using quiete a few For/Next loops

It got me thinking...
Instead of having this:

B4X:
For n = 0 to 5
label(n).initialize("")
Next
For n = 0 to 10
Btn(n).initialize("")
Next
For n = 0 to 15
dowhatever needs to be done
Next

How difficult would it be to have a single For/Next loop that would take in account the array dimension?
like
B4X:
For n = 0 to 15
If n < label.length then initialize labels
If n < btn.length then initialize btns
etc
Next

and above all, would it be code efficient?
 

ilan

Expert
Licensed User
Longtime User
B4X:
For n = 0 To 15
    Select n
        Case <= 5
            label(n).initialize("")
            Btn(n).initialize("")   
        Case <= 10
            Btn(n).initialize("")   
        Case Else
            'do nothing   
    End Select
    'dowhatever needs To be done from 0 to 15
Next
 

Cableguy

Expert
Licensed User
Longtime User
but if I remember right, only one case will be executed, even if more than one cases are within the scope of the operation
 

ilan

Expert
Licensed User
Longtime User
your code should also work if you have set the length of the array before you ask for it.

according to your code:
label array size should be 6
button array size should be 11
 

Cableguy

Expert
Licensed User
Longtime User
So, bottom line, is it more code efficient grouping 3 or more For/Next loops into one with conditional operations?
 

Ed Brown

Active Member
Licensed User
Longtime User
It's less efficient.

You have the conditional test of the FOR/NEXT and then you are adding another conditional test for each array.

But, if you're only dealing with a dozen or so components in each of the arrays then it's not that big a deal.
 

Cableguy

Expert
Licensed User
Longtime User
I may have in my current project a total of nearly 100 code created views, and the biggest view array is of 31 in length
 

ilan

Expert
Licensed User
Longtime User
So, bottom line, is it more code efficient grouping 3 or more For/Next loops into one with conditional operations?

its like:

peter, mark and fred are 3 brothers that goes every day on the same time to work.
peter work place is 15 minutes from home away, mark work place is 10 minutes from home away and fred work place is 5 minutes from home away.

all work places are in the same direction.
all 3 got a car but instead of everybody takes his own car and drive to work, peter could take mark and fred with him because their work place is in the same direction.

what do you say is this more efficient ?? ;)
 

Cableguy

Expert
Licensed User
Longtime User
My doubt resided in the fact that the "optimized" For/next may end up using more code lines than separate simple For/Next
 

ilan

Expert
Licensed User
Longtime User
My doubt resided in the fact that the "optimized" For/next may end up using more code lines than separate simple For/Next

if you finish the loop until the last number then it doesnot matter only if you would finish the loop before and exit it then mabe you are right.
but i think it is better to use 1 loop instead of 3 (less work for the processor)

what are you doing in those loops?
 

Cableguy

Expert
Licensed User
Longtime User
Basically initializing arrays of views and setting common properties.
 

imbault

Well-Known Member
Licensed User
Longtime User
Why not to do:
B4X:
For Each lbl As Label In label
    lbl.initialize("")
Next
For Each btn As Button In btn
    btn.Initialize("")
Next
 

Cableguy

Expert
Licensed User
Longtime User
I never tried that, not even sure it would work! Will definitely try it!
 

ilan

Expert
Licensed User
Longtime User
"for each.." is also faster then "for i = 0 to 10 ..."
but you will need more then 1 loop. i dont know if it would still be faster then use only 1 loop.
you will need to make the tests
 
Top