AS3 Vector of Arrays

Is there any performance advantage when storing arrays in an Array type vector?

e.g. option 1

private var _arrays:Vector.<Array> = new Vector.<Array>(2); _arrays[0] = new Array(10); _arrays[1] = new Array(10); 

Option 2

 private var _arrays:Array = new Array(2); _arrays[0] = new Array(10); _arrays[1] = new Array(10); 

Also do I have a vector or vectors?

  private var _vectors:Vector.<Vector> = new Vector.<Vector>(2); _vectors[0] = new Vector.<String>(10); _vectors[1] = new Vector.<String>(10); 

Thanks,

Mark

+6
source share
2 answers

EDIT

My original answer was wrong except for the very last part, and I have to apologize for that. I knew that Vector has exactly four "under the hood" implementations. (You can find decompiled sources from FP 10 playerglobal.swc in Robert Penner's post here ) Three of them are related to number types (int, uint and number). One for object types. This last one serves as a trick and accepts all classes derived from Object. That's why I suggested that Vector.<Object> is still faster than Array, relying on information about vectors and arrays available from Adobe .

However, it seems that this information is incorrect or, at least, it leaves some important parts:

  • While Vector.<AnyClassDerivedFromObject> allows strong typing, information of this type is evaluated only at compile time (so you get more type safety), but not at runtime - thus, in essence, the benefits of strict sets of object vectors do not apply to performance. See this blog post for more information.

  • Therefore, the only Vector implementations that are faster than Array are unique to number types (!).

In fact, I did some extensive testing on this and came to the conclusion that although Vector.<int> up to 60% faster than Array from int, all derivatives of Vector.<Object> not only equal in speed (i.e. Vector.<Object> performs the same actions as Vector.<String> , they are also about 20% slower than Array. I checked the double and triple check, so I think the results will be pretty accurate.

It is true that numeric type vectors are faster, so you should use them for performance advantages over an array. But:

End edit

Only if you are going to use sort() , sortOn() or any other of the convenient Array sort functions, you can still decide otherwise, because these are native functions and, as such, are very fast. Implementing Vector's own sorting methods will probably not match their speed.

+10
source

I personally do not see any performance benefits. If it is, it will be minimal. The whole point of having a vector class is to strictly enter the elements of the array. But the array object itself is designed to store several types of objects, or even untyped ones ... so strictly typing a vector into an almost untyped container that can be filled with untyped or multiple, differently typed contents ... with this thoughtful form it just sounds like this, as if it would be ineffective.

Update

Here are some performance test results to prove your point. We can see that when it is stressed, the vector of arrays is equal to performance as an array of arrays, and in one test case this is actually even worse:

 / ============================================================================================= Array Tests ============================================================================================= / Testing Array of Arrays push performance: Total time for 100000 push calls on Array of Arrays: 24 Testing Array of Arrays random assignment performance: Total time for 100000 random assignment calls on Array of Arrays: 40 Testing Array of Arrays sequential read performance: Total time for 100000 sequential read calls on Array of Arrays: 14 Testing Array of Arrays random read performance: Total time for 100000 random read calls on Array of Arrays: 41 / ============================================================================================= / / ============================================================================================= Vector Tests ============================================================================================= / Testing Vector of Arrays push performance: Total time for 100000 push calls on Vector of Arrays: 24 Testing Vector of Arrays random assignment performance: Total time for 100000 random assignment calls on Vector of Arrays: 49 Testing Vector of Arrays sequential read performance: Total time for 100000 sequential read calls on Vector of Arrays: 14 Testing Vector of Arrays random read performance: Total time for 100000 random read calls on Vector of Arrays: 41 / ============================================================================================= / 

And the test code:

 import flash.events.Event; import flash.utils.getTimer; //Performance timer related var startTime:Number; //ms // //Our two container types we're testing IO on var arrayOfArrays:Array = new Array(); var vectorOfArrays:Vector.<Array> = new Vector.<Array>(); // //Used to store a bunch of arrays we're going to use to test var testArrays:Array = new Array(); // var randomIndex:uint = 0; var i:uint = 0; var arr:Array; //Generate a bunch of arrays of mixed typed content for(i = 0; i < 100000; ++i) { generateTestArray(); } /*====================================================================================================== *********************************** Array Tests ********************************************* *=====================================================================================================*/ //Test push on array of arrays trace("Testing Array of Arrays push performance:"); startTime = getTimer(); for(i = 0; i < 100000; ++i) { arrayOfArrays.push(testArrays[i]); } trace("Total time for 100000 push calls on Array of Arrays: " + (getTimer() - startTime)); trace(" "); // //Test random write on array of arrays trace("Testing Array of Arrays random assignment performance:"); startTime = getTimer(); for(i = 0; i < 100000; ++i) { randomIndex = Math.round(Math.random() * 99999) as uint; arrayOfArrays[randomIndex] = testArrays[randomIndex]; } trace("Total time for 100000 random assignment calls on Array of Arrays: " + (getTimer() - startTime)); trace(" "); // //Test sequential read on array of arrays trace("Testing Array of Arrays sequential read performance:"); startTime = getTimer(); for(i = 0; i < 100000; ++i) { arr = arrayOfArrays[i]; } trace("Total time for 100000 sequential read calls on Array of Arrays: " + (getTimer() - startTime)); trace(" "); // //Test random read on array of arrays trace("Testing Array of Arrays sequential read performance:"); startTime = getTimer(); for(i = 0; i < 100000; ++i) { randomIndex = Math.round(Math.random() * 99999) as uint; arr = arrayOfArrays[randomIndex]; } trace("Total time for 100000 random read calls on Array of Arrays: " + (getTimer() - startTime)); trace(" "); // /*====================================================================================================*/ /*====================================================================================================== *********************************** Vector Tests ********************************************* *=====================================================================================================*/ //Test push on vector of arrays trace("Testing Vector of Arrays push performance:"); startTime = getTimer(); for(i = 0; i < 100000; ++i) { vectorOfArrays.push(testArrays[i]); } trace("Total time for 100000 push calls on Vector of Arrays: " + (getTimer() - startTime)); trace(" "); // //Test random write on vector of arrays trace("Testing Vector of Arrays random assignment performance:"); startTime = getTimer(); for(i = 0; i < 100000; ++i) { randomIndex = Math.round(Math.random() * 99999) as uint; vectorOfArrays[randomIndex] = testArrays[randomIndex]; } trace("Total time for 100000 random assignment calls on Vector of Arrays: " + (getTimer() - startTime)); trace(" "); // //Test sequential read on vector of arrays trace("Testing Vector of Arrays sequential read performance:"); startTime = getTimer(); for(i = 0; i < 100000; ++i) { arr = vectorOfArrays[i]; } trace("Total time for 100000 sequential read calls on Vector of Arrays: " + (getTimer() - startTime)); trace(" "); // //Test random read on vector of arrays trace("Testing Vector of Arrays sequential read performance:"); startTime = getTimer(); for(i = 0; i < 100000; ++i) { randomIndex = Math.round(Math.random() * 99999) as uint; arr = vectorOfArrays[randomIndex]; } trace("Total time for 100000 random read calls on Vector of Arrays: " + (getTimer() - startTime)); trace(" "); // /*====================================================================================================*/ function generateTestArray():void { var newArray:Array = new Array(); var totalItems:uint = Math.round(Math.random() * 50 + 1); var i:uint = 0; var dice:uint = 0; for(i; i < totalItems; ++i) { dice = Math.round(Math.random() * 5); switch(dice) { case 0: newArray.push(new int(Math.random())); break; case 1: newArray.push(new String(Math.random())); break; case 2: newArray.push(new Array()); break; case 3: newArray.push(new MovieClip()); break; case 4: newArray.push(new Date()); break; case 5: newArray.push(new Event(Event.COMPLETE, false, false)); break; } } testArrays.push(newArray); } 
+6
source

Source: https://habr.com/ru/post/904019/


All Articles