7 Replies Latest reply on Aug 19, 2009 8:45 PM by Preetham Hegde

# Efficient Looping

Is there a more efficient way to do this sort of looping within a loop? Dictionary??

for (var i:int = 0; i < myAC.length; i++) {
if (myAC[i].cPosition > myAC[i].cCount) {
for (var ii:int = 0; ii < myAC.length; ii++) {
if (myAC[ii].apID != myAC[i].apID && myAC[i].Group == myAC[ii].Group && myAC[ii].y > myAC[i].y) {
myAC[i].cPosition = myAC[ii - 1].cPosition;
}
}
}
for (var iii:int = 0; iii < myAC.length; iii++) {
if (myAC[iii].apID != myAC[i].apID && myAC[i].Group == myAC[iii].Group && myAC[i].cPosition == myAC[iii].cPosition && myAC[iii].y >= myAC[i].y && myAC[iii].y <= myAC[i].y) {
myAC[iii].cPosition2 = myAC[i].cPosition + 1;
}
}
}

• ###### 1. Re: Efficient Looping

Any suggestions/thoughts on how to more efficiently do this type of looping within a loop?

for (var i:int = 0; i < myAC.length; i++) {
if (myAC[i].a > myAC[i].b) {
for (var ii:int = 0; ii < myAC.length; ii++) {
if (myAC[ii].c != myAC[i].c && myAC[i].d == myAC[ii].d) {
myAC[i].c = myAC[ii - 1].c;
}
}
}
}

• ###### 2. Re: Efficient Looping

Does this work in all condition...

If ii = 0 and i = 0,  and if below condition pass, it will throw exception..

..

...

if (myAC[ii].c != myAC[i].c && myAC[i].d == myAC[ii].d) {
myAC[i].c = myAC[ii - 1].c;
}

...

..

• ###### 3. Re: Efficient Looping

Yes, this always works. What I'm really after is a better way to loop. Instead of a sub-loop through my ArrayCollection for every item in the ArrayCollection.

• ###### 4. Re: Efficient Looping

If all items are unique in myAC:

<?xml version="1.0" encoding="utf-8"?>
creationComplete="init();">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;

[Bindable] private var myAC:ArrayCollection = new ArrayCollection([
{cPosition: 0, cCount: 0, apID: 1, Group: "Group One", y: 0},
{cPosition: 1, cCount: 1, apID: 2, Group: "Group Two", y: 10},
{cPosition: 2, cCount: 2, apID: 3, Group: "Group Three", y: 20},
{cPosition: 3, cCount: 3, apID: 4, Group: "Group Four", y: 30},
{cPosition: 0, cCount: 0, apID: 1, Group: "Group One", y: 0},
{cPosition: 1, cCount: 1, apID: 2, Group: "Group Two", y: 10},
{cPosition: 2, cCount: 2, apID: 3, Group: "Group Three", y: 20},
{cPosition: 3, cCount: 3, apID: 4, Group: "Group Four", y: 30}
]);

private function init():void{
for each(var obj1:Object in myAC) {
if (obj1.cPosition > obj1.cCount) {
for each(var obj2:Object in myAC) {
if (obj2.apID != obj1.apID && obj1.Group == obj2.Group && obj2.y > obj1.y) {
obj1.cPosition = myAC.getItemAt(myAC.getItemIndex(obj2) - 1).cPosition;
}
}
}
for each(var obj3:Object in myAC) {
if (obj3.apID != obj1.apID && obj1.Group == obj3.Group &&
obj1.cPosition == obj3.cPosition && obj3.y >= obj1.y && obj3.y <= obj1.y) {
obj3.cPosition2 = obj1.cPosition + 1;
}
}
}
}
]]>
</mx:Script>
</mx:Application>

And if all items are not unique:

<?xml version="1.0" encoding="utf-8"?>
creationComplete="init();">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;

[Bindable] private var myAC:ArrayCollection = new ArrayCollection([
{cPosition: 0, cCount: 0, apID: 1, Group: "Group One", y: 0},
{cPosition: 1, cCount: 1, apID: 2, Group: "Group Two", y: 10},
{cPosition: 2, cCount: 2, apID: 3, Group: "Group Three", y: 20},
{cPosition: 3, cCount: 3, apID: 4, Group: "Group Four", y: 30},
{cPosition: 0, cCount: 0, apID: 1, Group: "Group One", y: 0},
{cPosition: 1, cCount: 1, apID: 2, Group: "Group Two", y: 10},
{cPosition: 2, cCount: 2, apID: 3, Group: "Group Three", y: 20},
{cPosition: 3, cCount: 3, apID: 4, Group: "Group Four", y: 30}
]);

private function init():void{
for each(var obj1:Object in myAC) {
if (obj1.cPosition > obj1.cCount) {
for (var ii:int = 0; ii < myAC.length; ii++) {
if (myAC[ii].apID != obj1.apID && obj1.Group == myAC[ii].Group && myAC[ii].y > obj1.y) {
obj1.cPosition = myAC[ii - 1].cPosition;
}
}
}
for each(var obj3:Object in myAC) {
if (obj3.apID != obj1.apID && obj1.Group == obj3.Group &&
obj1.cPosition == obj3.cPosition && obj3.y >= obj1.y && obj3.y <= obj1.y) {
obj3.cPosition2 = obj1.cPosition + 1;
}
}
}
}
]]>
</mx:Script>
</mx:Application>

• ###### 5. Re: Efficient Looping

All items in the array are unique. Is there a performance difference between the "for each" and the "for" loop?:

for each(var obj1:Object in myAC) {
if (obj1.cPosition > obj1.cCount) {
for each(var obj2:Object in myAC) {
if (obj2.apID != obj1.apID && obj1.Group == obj2.Group && obj2.y > obj1.y) {
obj1.cPosition = myAC.getItemAt(myAC.getItemIndex(obj2) - 1).cPosition;
}

OR

for (var i:int = 0; i < myAC.length; i++) {....

• ###### 6. Re: Efficient Looping

Not sure, but seems like for each would be more efficient.