Flash AS3阵列向量
在数组类型的向量中存储数组是否有任何性能优势 e、 g备选方案1Flash AS3阵列向量,flash,actionscript-3,Flash,Actionscript 3,在数组类型的向量中存储数组是否有任何性能优势 e、 g备选方案1 private var _arrays:Vector.<Array> = new Vector.<Array>(2); _arrays[0] = new Array(10); _arrays[1] = new Array(10); 也可以给我一个或多个向量吗 private var _vectors:Vector.<Vector> = new Vector.<Vector>(
private var _arrays:Vector.<Array> = new Vector.<Array>(2);
_arrays[0] = new Array(10);
_arrays[1] = new Array(10);
也可以给我一个或多个向量吗
private var _vectors:Vector.<Vector> = new Vector.<Vector>(2);
_vectors[0] = new Vector.<String>(10);
_vectors[1] = new Vector.<String>(10);
private var\u vectors:Vector.=新载体(2);
_向量[0]=新向量(10);
_向量[1]=新向量(10);
谢谢
马克我个人看不到任何性能优势。如果有,它将是最小的。拥有vector类的全部意义在于严格地键入数组的元素。但是,数组对象本身被设计用来容纳多种类型的对象,甚至是非类型的对象。。。因此,严格地将一个向量输入到一个本质上没有类型的容器中,该容器可以填充没有类型的或多个不同类型的内容。。。如果这样想的话,从逻辑上讲,它几乎没有效果 更新 下面是一些性能测试结果来证明我的观点。我们可以看到,当受到压力时,数组向量等于数组的性能,在一个测试用例中,它实际上更糟糕:
/ =============================================================================================
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
/ ============================================================================================= /
以及测试代码:
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);
}
导入flash.events.Event;
导入flash.utils.getTimer;
//性能定时器相关
var startTime:数字//太太
//
//我们正在测试IO的两种容器类型
var arrayOfArrays:Array=new Array();
阵列向量:向量=新向量();
//
//用于存储一组我们将用于测试的阵列
var testarray:Array=new Array();
//
var随机指数:uint=0;
变量i:uint=0;
var-arr:数组;
//生成一组混合类型内容的数组
对于(i=0;i<100000;++i){
generateTestArray();
}
/*======================================================================================================
***********************************阵列测试*********************************************
*=====================================================================================================*/
//阵列的测试推送阵列
跟踪(“测试阵列的阵列推送性能:”);
startTime=getTimer();
对于(i=0;i<100000;++i){
arrayOfArrays.push(testarray[i]);
}
trace(“对数组数组进行100000次推送调用的总时间:”+(getTimer()-startTime));
痕迹(“”);
//
//在数组的数组上测试随机写入
trace(“测试数组的随机分配性能:”);
startTime=getTimer();
对于(i=0;i<100000;++i){
randomIndex=Math.round(Math.random()*99999)作为uint;
阵列法拉利[randomIndex]=测试阵列[randomIndex];
}
trace(“对数组数组进行100000次随机赋值调用的总时间:”+(getTimer()-startTime));
痕迹(“”);
//
//在数组的数组上测试顺序读取
跟踪(“测试阵列的阵列顺序读取性能:”);
startTime=getTimer();
对于(i=0;i<100000;++i){
arr=阵列法拉利[i];
}
trace(“数组数组上100000个顺序读取调用的总时间:”+(getTimer()-startTime));
痕迹(“”);
//
//在数组的数组上测试随机读取
跟踪(“测试阵列的阵列顺序读取性能:”);
startTime=getTimer();
对于(i=0;i<100000;++i){
randomIndex=Math.round(Math.random()*99999)作为uint;
arr=阵列法拉利[随机指数];
}
trace(“对数组数组进行100000次随机读取调用的总时间:”+(getTimer()-startTime));
痕迹(“”);
//
/*====================================================================================================*/
/*======================================================================================================
***********************************病媒测试*********************************************
*=====================================================================================================*/
//阵列的测试推送向量
trace(“阵列推送性能的测试向量:”);
startTime=getTimer();
对于(i=0;i<100000;++i){
矢量阵列推(testarray[i]);
}
trace(“对数组向量进行100000次推送调用的总时间:”+(getTimer()-startTime));
痕迹(“”);
//
//测试数组向量的随机写入
trace(“阵列随机分配性能测试向量:”);
startTime=getTimer();
对于(i=0;i<100000;++i){
randomIndex=Math.round(Math.random()*99999)作为uint;
矢量阵列[randomIndex]=测试阵列[randomIndex];
}
trace(“数组向量上100000个随机赋值调用的总时间:”+(getTimer()-startTime));
痕迹(“”);
//
//测试阵列向量的顺序读取
trace(“阵列顺序读取性能测试向量:”);
startTime=getTimer();
对于(i=0;i<100000;++i){
arr=阵列向量[i];
}
trace(“数组向量上100000个顺序读取调用的总时间:”+(getTimer()-startTime));
痕迹(“”);
//
//测试阵列向量的随机读取
trace(“阵列顺序读取性能测试向量:”);
startTime=getTimer();
对于(i=0;i<100000;++i){
randomIndex=Math.round(Math.random()*99999)作为uint;
arr=阵列向量[randomIndex];
}
trace(“数组向量上100000个随机读取调用的总时间:”+(getTimer()-startTime));
痕迹(“”);
//
/*====================================================================================================*/
函数generateTestArray():void
{
var newArray:Array=newArray();
var totalItems:uint=Math.round(Math.random()*50+1);
变量i:uint=0;
变量骰子:uint=0;
对于(i;iimport 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);
}