使い回しVector配列とnew Vector
配列の長さによって速度差が変わるのはなぜだろう。
♥0 |
Line 177 |
Modified 2015-01-30 15:16:32 |
MIT License
archived:2017-03-20 08:06:42
ActionScript3 source code
/**
* Copyright yasurageruheya ( http://wonderfl.net/user/yasurageruheya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/mtcJ
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
public class FlashTest extends Sprite {
public const LOOP:int = 10;
public const txt:TextField = new TextField();
public const const_vec:Vector.<TestObject> = new Vector.<TestObject>();
public var var_vec:Vector.<TestObject>;
public var tests:Vector.<Tester> = new Vector.<Tester>();
public function FlashTest() {
tests[tests.length] = new Tester("length = 10 : ", function():void
{
var i:int = LOOP;
const length:int = 10;
var j:int;
while (i--)
{
const_vec.length = 0;
const_vec.length = length;
j = length;
while (j--)
{
const_vec[j] = new TestObject();
}
}
});
tests[tests.length] = new Tester("new(10) : ", function():void
{
var i:int = LOOP;
const length:int = 10;
var j:int;
while (i--)
{
var_vec = new Vector.<TestObject>(length);
j = length;
while (j--)
{
const_vec[j] = new TestObject();
}
}
});
tests[tests.length] = new Tester("length = 100 : ", function():void
{
var i:int = LOOP;
const length:int = 100;
var j:int;
while (i--)
{
const_vec.length = 0;
const_vec.length = length;
j = length;
while (j--)
{
const_vec[j] = new TestObject();
}
}
});
tests[tests.length] = new Tester("new(100) : ", function():void
{
var i:int = LOOP;
const length:int = 100;
var j:int;
while (i--)
{
var_vec = new Vector.<TestObject>(length);
j = length;
while (j--)
{
const_vec[j] = new TestObject();
}
}
});
tests[tests.length] = new Tester("length = 1000 : ", function():void
{
var i:int = LOOP;
const length:int = 1000;
var j:int;
while (i--)
{
const_vec.length = 0;
const_vec.length = length;
j = length;
while (j--)
{
const_vec[j] = new TestObject();
}
}
});
tests[tests.length] = new Tester("new(1000) : ", function():void
{
var i:int = LOOP;
const length:int = 1000;
var j:int;
while (i--)
{
var_vec = new Vector.<TestObject>(length);
j = length;
while (j--)
{
const_vec[j] = new TestObject();
}
}
});
tests[tests.length] = new Tester("length = 5000 : ", function():void
{
var i:int = LOOP;
const length:int = 5000;
var j:int;
while (i--)
{
const_vec.length = 0;
const_vec.length = length;
j = length;
while (j--)
{
const_vec[j] = new TestObject();
}
}
});
tests[tests.length] = new Tester("new(5000) : ", function():void
{
var i:int = LOOP;
const length:int = 5000;
var j:int;
while (i--)
{
var_vec = new Vector.<TestObject>(length);
j = length;
while (j--)
{
const_vec[j] = new TestObject();
}
}
});
tests.reverse();
txt.width = 500;
txt.height = 500;
addChild(txt);
// write as3 code here..
addEventListener(Event.ENTER_FRAME, test);
}
private function test(e:Event):void
{
var str:String = "";
var i:int = tests.length;
while (i--)
{
str += tests[i].start();
}
txt.text = str;
}
}
}
class TestObject
{
public function TestObject(){}
}
import flash.utils.getTimer;
class Tester
{
public var time:Number = 0;
public var name:String;
public var test:Function;
public var count:int = 1;
public function start():String
{
const startTime:int = getTimer();
test();
time += getTimer() - startTime;
return name + (time / (count++)) + " ms.\n";
}
public function Tester(name:String, test:Function)
{
this.name = name;
this.test = test;
}
}