Radom Access: Array vs Dictionary vs Object
1. キーは数値である
2. キーは連続するとは限らない
3. 未定義領域へのランダムアクセスを許容する
Object先生
キーが連続値であり、ちゃんと定義された領域のみにアクセスする場合 http://wonderfl.net/c/fRSD
/**
* Copyright alumican_net ( http://wonderfl.net/user/alumican_net )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/AngK
*/
package {
import flash.display.*;
import flash.text.*;
import flash.utils.*;
public class Main extends Sprite
{
public function Main():void
{
_setup();
var n:int = 1000000;
var ary:Array;
var dic:Dictionary;
var obj:Object;
var idx:int;
var val:Boolean;
ary = new Array();
_measure("Array : write", function ():void
{
idx = int(Math.random() * 1000);
ary[idx] = true;
}, n);
ary = new Array();
_measure("Array : read", function ():void
{
idx = int(Math.random() * 1000);
val = ary[idx];
}, n);
ary = new Array();
_measure("Array : delete", function ():void
{
idx = int(Math.random() * 1000);
ary.splice(idx, 1);
}, n);
dic = new Dictionary();
_measure("Dictionary : write", function ():void
{
idx = int(Math.random() * 1000);
dic[idx] = true;
}, n);
dic = new Dictionary();
_measure("Dictionary : read", function ():void
{
idx = int(Math.random() * 1000);
val = dic[idx];
}, n);
_measure("Dictionary : delete", function ():void
{
idx = int(Math.random() * 1000);
delete dic[idx];
}, n);
obj = new Object();
_measure("Object : write", function ():void
{
idx = int(Math.random() * 1000);
obj[idx] = true;
}, n);
obj = new Object();
_measure("Object : read", function ():void
{
idx = int(Math.random() * 1000);
val = obj[idx];
}, n);
_measure("Object : delete", function ():void
{
idx = int(Math.random() * 1000);
delete obj[idx];
}, n);
}
private function _measure(
title:String, func:Function, numTimes:uint, ...params):void
{
_time = getTimer();
for (var i:int = 0; i < numTimes; i++) {
func.apply(null, params);
}
_time = getTimer() - _time;
_debug("[ " + title + " ] --> " + _time + " ms");
}
private function _debug(log:String):void
{
_field.appendText(log + "\n");
}
private var _field:TextField;
private var _time:uint;
private function _setup():void
{
_field = new TextField();
_field.width = stage.stageWidth;
_field.height = stage.stageHeight;
var format:TextFormat = _field.defaultTextFormat;
format.font = "_sans";
_field.defaultTextFormat = format;
addChild(_field);
}
}
}