cro*_*y88 6 flash actionscript-3
将数组存储在Array类型的Vector中是否有任何性能优势?
例如选项1
private var _arrays:Vector.<Array> = new Vector.<Array>(2);
_arrays[0] = new Array(10);
_arrays[1] = new Array(10);
Run Code Online (Sandbox Code Playgroud)
选项2
private var _arrays:Array = new Array(2);
_arrays[0] = new Array(10);
_arrays[1] = new Array(10);
Run Code Online (Sandbox Code Playgroud)
我也可以有矢量或矢量吗?
private var _vectors:Vector.<Vector> = new Vector.<Vector>(2);
_vectors[0] = new Vector.<String>(10);
_vectors[1] = new Vector.<String>(10);
Run Code Online (Sandbox Code Playgroud)
谢谢,
标记
wel*_*rat 10
编辑
我的原始答案是错误的,除了最后一部分,我必须为此道歉.我知道Vector在"引擎盖下"有四个实现.(你可以找到从FP 10的playerglobal.swc在后由罗伯特·彭纳反编译源在这里)那些三是号码类型(INT,UINT和数字).一个是对象类型.最后一个用作catch-all并接受从Object派生的所有类.这就是为什么我认为它Vector.<Object>仍然比Array更快,依赖于Adobe提供的有关矢量和数组的信息.
但是,似乎这些信息是错误的,或者至少它遗漏了一些重要的部分:
虽然Vector.<AnyClassDerivedFromObject>允许严格键入,但此类型信息仅在编译时评估(因此您获得更多类型安全性),但不在运行时评估 - 因此,严格键入对象向量的好处实际上不适用于性能.有关详细信息,请参阅此博客文章.
因此,Vector中比Array快的唯一实现是数字类型(!).
事实上,我已经对此进行了一些广泛的测试,并且得出的结论是,虽然Vector.<int>比int of int快60%,但所有的衍生物Vector.<Object>不仅速度Vector.<Object>相同(即执行相同Vector.<String>,它们也是比Array 低约20%.我对此进行了双重和三重检查,因此我认为结果相当准确.
数字类型向量仍然更快,因此您应该使用这些向量而不是数组.但:
结束编辑
只有当你要使用sort(),sortOn()或任何其它的阵列的方便排序功能,您可能还是会另作决定,因为这些都是原生功能,这样真的快.在Vector上实现自己的排序方法可能与它们的速度不匹配.
小智 6
我个人看不到任何性能优势.如果有,它将是最小的.拥有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
/ ============================================================================================= /
Run Code Online (Sandbox Code Playgroud)
和测试代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3072 次 |
| 最近记录: |