2011-12-18 9 views
6

配列を配列型のVectorに格納すると、パフォーマンス上の利点はありますか?AS3配列のベクトル

例えばオプション1

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

オプション2

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

はまた、私は、Vectorやベクトルを持つことができますか?

private var _vectors:Vector.<Vector> = new Vector.<Vector>(2); 

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

おかげで、

マーク

+0

更新された回答@ crooksy88を参照してください。私は、私のオリジナルが正しいことを示し、weltraumpiratの答えが間違っていることを示すテストコード+結果を投稿しました。 –

答えて

10

EDIT

私のオリジナルの答えは非常に最後の部分を除いて間違っていた、と私はそのために謝罪しなければなりません。私はVectorがちょうど4つの実装を「フードの下に」持っているという事実を知っていました。 (FP10 playerglobal.swcの逆コンパイルされたソースは、Robert Pennerの投稿でhereにあります)。そのうち3つは数値型(int、uint、Number)です。 1つはオブジェクト型です。最後の1つはcatch-allとして機能し、Objectから派生したすべてのクラスを取り込みます。このため、私はVector.<Object>the information regarding vectors and arrays available from Adobeに依拠してもまだアレイより高速であると仮定していたのです。

しかし、この情報が間違っているようだ、あるいは少なくともそれはいくつかの重要な部分を残し:

  1. Vector.<AnyClassDerivedFromObject>は、厳密な型指定が可能になりますが、このタイプの情報のみ、コンパイル時に評価(だかられます型の安全性が増します)、実行時ではないため、本質的に厳密な型付きオブジェクトベクトルの利点は性能には適用されません。詳細については、this blog postを参照してください。

  2. したがって、配列よりも高速なVectorの実装は、数値型(!)のものだけです。

は実際に、私はこの上でいくつかの大規模なテストを行っている、とVector.<int>はintの配列よりも最大60%高速である一方で、Vector.<Object>の全ての誘導体は、高速で唯一の等しくないという結論になってきました(すなわち、Vector.<Object>は、Vector.<String>と同じことを実行しますが、アレイよりも約20%遅いです。これを二重と三重にチェックしたので、結果はかなり正確だと私は信じています。

数値型のベクトルが高速であることは変わりません。したがって、これらの数値型のベクトルは、配列よりもパフォーマンスの面で優れているはずです。しかし:

としてあなたがsort()sortOn()を使用するつもりか、これらはネイティブ関数であるため、アレイの便利なソート機能の他には、あなたはまだ、それ以外決めるかもしれません、そして場合のみのEND EDIT

そのようなは本当に高速です。 Vector上に独自のソートメソッドを実装すると、おそらく速度に合わないでしょう。

+1

私は 'Vector。 'は' Array'より速いことがあります。これがなぜであるかを詳しく説明できますか? –

6

私は個人的に任意のパフォーマンス上の利点を見ることができません。もしあれば、最小限に抑えられます。ベクトルクラスを持つ全ポイントは、配列の要素を厳密に型付けすることです。しかし、配列オブジェクト自体は、複数の型のオブジェクトを保持するように設計されています。型指定されていなくても...型指定されていない複数の型付きのコンテンツでいっぱいになる型付けされていないコンテナにベクトルを厳密に型付けします。論理的にはほとんど効果がないように聞こえるだけです。

更新

ここに私のポイントを証明するためにいくつかのパフォーマンステストの結果です。私たちは、ストレス下に置かれたときに、配列のベクトルは、配列の配列としての性能に等しいことを確認することができ、1つのテストケースでは、それは実際にはさらに悪いです:

/ ============================================================================================= 
            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); 
} 
関連する問題