Optimisation of the For/Next loops (Tutorial Request)

Discussion in 'Teaching Programming with B4X' started by Cableguy, Jan 22, 2016.

  1. Cableguy

    Cableguy Expert Licensed 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:

    Code:
    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
    Code:
    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?
     
  2. ilan

    ilan Expert Licensed User

    Code:
    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
     
  3. Cableguy

    Cableguy Expert Licensed 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
     
    DonManfred likes this.
  4. ilan

    ilan Expert Licensed User

    i repaired my code after 5 sec i wrote it but you where faster :D
     
    DonManfred and Cableguy like this.
  5. ilan

    ilan Expert Licensed 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
     
  6. Cableguy

    Cableguy Expert Licensed User

    So, bottom line, is it more code efficient grouping 3 or more For/Next loops into one with conditional operations?
     
  7. Ed Brown

    Ed Brown Active Member Licensed 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.
     
  8. Cableguy

    Cableguy Expert Licensed 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
     
  9. Ed Brown

    Ed Brown Active Member Licensed User

    You should be fine with those numbers.
    :)
     
    Cableguy likes this.
  10. ilan

    ilan Expert Licensed User

    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 likes this.
  11. Cableguy

    Cableguy Expert Licensed User

    My doubt resided in the fact that the "optimized" For/next may end up using more code lines than separate simple For/Next
     
  12. ilan

    ilan Expert Licensed User

    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?
     
  13. Cableguy

    Cableguy Expert Licensed User

    Basically initializing arrays of views and setting common properties.
     
  14. ilan

    ilan Expert Licensed User

    in this case i would use only 1 loop to do that
     
    Cableguy likes this.
  15. imbault

    imbault Well-Known Member Licensed User

    Why not to do:
    Code:
    For Each lbl As Label In label
        lbl.initialize(
    "")
    Next
    For Each btn As Button In btn
        btn.Initialize(
    "")
    Next
     
  16. Cableguy

    Cableguy Expert Licensed User

    I never tried that, not even sure it would work! Will definitely try it!
     
  17. imbault

    imbault Well-Known Member Licensed User

    For sure it works, I usually do that way
     
    Cableguy likes this.
  18. ilan

    ilan Expert Licensed 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
     
    wonder likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice