简体   繁体   中英

AS3 Performance: Many Arrays vs One Array + 'is'

I have quite alots of display objects to manage during runtime, so Im currently using Arrays to manage them. But the problem is I have few many types of displays objects(eg. Tile, Npc and Building) which are basically MovieClips linked to the library. Each display object plays different role where it will be checked on enter frame, a loop.

Method 2 sounds much more faster and extensible however Im worried if it would affect the checking rate of each display object during runtime as the displays:Array grow larger and probably making it glitchy

So which one of the following method is faster+less glitchy and explain why you choose it.

Thanks in advance.

Method 1

var tiles:Array = new Array()
var npcs:Array = new Array()
var buildings:Array = new Array()

function createTiles(){
for(var i:Number=0; i<10; i++){
var t:Tile = new Tile() //Display object in the library
t.x = i * 50
t.y = i * 50
addChild(t)
tiles.push(t)
}
}

function createNpcs(){...}
function createBuildings(){...}

addEventListener(Event.ENTER_FRAME, loop)

function loop(e:Event){
for(var i:Number=0; i<tiles.length; i++){
//some codes
}
for(var j:Number=0; j<npcs.length; j++){
//some codes
}
for(var k:Number=0; k<buildings.length; k++){
//some codes
}
}

Method 2

var displays:Array = new Array();

function createDispalys(){
for(var i:Number=0; i<10; i++){
var t:Tile = new Tile() //Display object in the library
t.x = i * 50
t.y = i * 50
addChild(t)
displays.push(t)
}
for(var j:Number=0; j<10; j++){
//some code
displays.push(t)
}
for(var k:Number=0;k<10; k++){
//some codes
displays.push(t)
}
}
addEventListener(Event.ENTER_FRAME, loop)
function loop(e:Event){
for(var i:Number=0; i<display.length; i++){
if(display[i] is Tile){
//some codes
}else if(display[i] is Npc){
//some codes
}else if(display[i] is Building){
//some codes
}
}
}

Have you considered refactoring method 2 to put the logic inside the class itself?

Ie:

public interface DisplayAsset
{
     void onEnterFrame();
}

// Implementations ommitted
public class Npc implements DisplayAsset {}
public class Tile implements DisplayAsset {}
public class Building implements DisplayAsset {}

Then, your loop remains extensible (just add another DisplayAsset impl.), and fast -- your code becomes:

var displays:Array = new Array();  // As per Gregor Kiddie's comment, use a vector here if possible
// as a vector:
// var displays:Vector.<DisplayAsset> = new Vector.<DisplayAsset>();

function createDispalys(){
   for(var i:Number=0; i<10; i++){
     var t:Tile = new Tile() //Display object in the library
     t.x = i * 50
     t.y = i * 50
     addChild(t)
     displays.push(t)
   }
   for(var j:Number=0; j<10; j++){
       //some code
       displays.push(t)
    }
    for(var k:Number=0;k<10; k++){
       //some codes
       displays.push(t)
    }
}
addEventListener(Event.ENTER_FRAME, loop)
function loop(e:Event){
     for each (var displayAsset:DisplayAsset in displays)
     {
          displayAsset.onEnterFrame();
     }
 }

如果性能是您追求的,则应该改用Vector

What about extending those classes (Tile, Npc, Building), from one basic class - let's say TypeClass , which has an overwritable method getType() .

This method returns "None" by default, but by expanding the class and overwriting the method it can return "Tile", "Npc" or "Building"...

With this, it is possible to make switch statements, and basically easier to manage code...

switch (anObject.getType()){
    case "etc"...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM