Ok just first of all, I would like to point out that the method in my previous post was in all actuality not a recursive method. It loops through objects in that class but not the same object consecutive times.. my bad.
On that note. Thanks to Hudson Ansley on the flashcodersny blog for pointing out that I could instead of using an array to loop through and what not I could instead use a Dictionary object. Here is the final fixed code:
public function addNeighborToDictionary(item:CityOnMap):void
{
if(this.neighborsDict[item]==undefined)
{
this.neighborsDict[item] = item;
for (var key:Object in item.neighborsDict)
{
CityOnMap(item.neighborsDict[key]).addNeighborToDictionary(this)
}
item.neighborsDict[this] = this;
}
}
This is not only much shorter code, but in a list of 159 properties it cut down the time by approx. 200ms.
At 84 items there was no real noticeable difference in time with both methods clocking in at about 350ms. With 159 items with identical number of neighbors, it starts to get noticeable with the neighbors Array method taking approx. 600-700 ms while the neighbors Dictionary method completed in approx. 300-350 ms.
I can only imagine that as the items increase in number the difference would continue to rise.
Leave a Reply
You must be logged in to post a comment.