Cableguy Expert Licensed User Longtime User Jan 22, 2016 #1 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: Copy 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: Copy 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?
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: Copy 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: Copy 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 Jan 22, 2016 #2 Code: Copy 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
Code: Copy 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 Jan 22, 2016 #3 but if I remember right, only one case will be executed, even if more than one cases are within the scope of the operation
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 Jan 22, 2016 #4 Cableguy said: but if I remember right, only one case will be executed, even if more than one cases are within the scope of the operation Click to expand... i repaired my code after 5 sec i wrote it but you where faster
Cableguy said: but if I remember right, only one case will be executed, even if more than one cases are within the scope of the operation Click to expand... i repaired my code after 5 sec i wrote it but you where faster
ilan Expert Licensed User Longtime User Jan 22, 2016 #5 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
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 Jan 22, 2016 #6 So, bottom line, is it more code efficient grouping 3 or more For/Next loops into one with conditional operations?
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 Jan 22, 2016 #7 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.
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 Jan 22, 2016 #8 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
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
Ed Brown Active Member Licensed User Longtime User Jan 22, 2016 #9 You should be fine with those numbers.
ilan Expert Licensed User Longtime User Jan 23, 2016 #10 Cableguy said: So, bottom line, is it more code efficient grouping 3 or more For/Next loops into one with conditional operations? Click to expand... 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 said: So, bottom line, is it more code efficient grouping 3 or more For/Next loops into one with conditional operations? Click to expand... 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 Jan 23, 2016 #11 My doubt resided in the fact that the "optimized" For/next may end up using more code lines than separate simple For/Next
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 Jan 23, 2016 #12 Cableguy said: My doubt resided in the fact that the "optimized" For/next may end up using more code lines than separate simple For/Next Click to expand... 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 said: My doubt resided in the fact that the "optimized" For/next may end up using more code lines than separate simple For/Next Click to expand... 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 Jan 23, 2016 #13 Basically initializing arrays of views and setting common properties.
ilan Expert Licensed User Longtime User Jan 23, 2016 #14 in this case i would use only 1 loop to do that
imbault Well-Known Member Licensed User Longtime User Jan 23, 2016 #15 Why not to do: Code: Copy For Each lbl As Label In label lbl.initialize("") Next For Each btn As Button In btn btn.Initialize("") Next
Why not to do: Code: Copy 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 Jan 23, 2016 #16 I never tried that, not even sure it would work! Will definitely try it!
imbault Well-Known Member Licensed User Longtime User Jan 23, 2016 #17 For sure it works, I usually do that way
ilan Expert Licensed User Longtime User Jan 23, 2016 #18 "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
"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