最適化
結論:かわらない
[最適化 Tips] 変数名の長さによる処理速度の違い withありなしの処理速度の違い
- // forked from muta244's [最適化 Tips] 変数名の長さによる処理速度の違い
- package {
- import flash.display.*;
- import flash.events.*;
- import flash.text.*;
- import flash.utils.*;
- public class Main extends Sprite
- {
- static private const _NUM_TIMES:uint = 10000000;
- private function _init():void
- {
- _debug(
- "各テスト " + _NUM_TIMES + " 回処理させた計算結果 [単位 : ミリ秒]\n" +
- "(誤差は多少生じます)\n"
- );
- _measure("ループのみ", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- }
- });
- var sample:Sample = new Sample(10, 20, 30);
- sample.child = new Sample(10, 20, 30);
- sample.child.child = new Sample(10, 20, 30);
- _measure("withなし 自プロパティ", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- sample.data1;
- sample.data2;
- sample.data3;
- }
- });
- _measure("withあり 自プロパティ", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- with (sample)
- {
- data1;
- data2;
- data3;
- }
- }
- });
- _measure("withなし 孫プロパティ", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- sample.child.child.data1;
- sample.child.child.data2;
- sample.child.child.data3;
- }
- });
- _measure("withあり 孫プロパティ", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- with (sample.child.child)
- {
- data1;
- data2;
- data3;
- }
- }
- });
- _measure("withなし 子&孫プロパティ", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- sample.child.data1;
- sample.child.data2;
- sample.child.data3;
- sample.child.child.data1;
- sample.child.child.data2;
- sample.child.child.data3;
- }
- });
- _measure("withあり 子&孫プロパティ", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- with (sample.child)
- {
- data1;
- data2;
- data3;
- child.data1;
- child.data2;
- child.data3;
- }
- }
- });
- _debug("\n結果については言及しませんので, 各自ご判断ください.");
- }
- private var _field:TextField;
- private var _time:uint;
- public function Main():void
- {
- _setup();
- _init();
- }
- private function _measure(title:String, func:Function, ...params):void
- {
- _time = getTimer();
- func.apply(null, params);
- _time = getTimer() - _time;
- _debug("[ " + title + " ] --> " + _time + " ms");
- }
- private function _debug(log:String):void
- {
- _field.appendText(log + "\n");
- }
- private function _setup():void
- {
- _field = new TextField();
- _field.width = stage.stageWidth - 40;
- _field.height = stage.stageHeight - 60;
- _field.x = 20;
- _field.y = 60;
- var format:TextFormat = _field.defaultTextFormat;
- format.font = "_sans";
- _field.defaultTextFormat = format;
- addChild(_field);
- var button:Sprite = new Sprite();
- button.graphics.lineStyle(1, 0xBBBBBB);
- button.graphics.beginFill(0xEEEEEE);
- button.graphics.drawRoundRect(0, 0, 100, 20, 5, 5);
- button.graphics.endFill();
- addChild(button);
- button.x = 20;
- button.y = 20;
- button.mouseChildren = false;
- button.buttonMode = true;
- var field:TextField = new TextField();
- field.width = 100;
- field.height = 20;
- field.htmlText = "<p align='center'><font face='_sans'>再計算</span></p>";
- button.addChild(field);
- button.addEventListener(MouseEvent.CLICK, function ():void
- {
- _field.text = "";
- _init();
- });
- }
- }
- }
- class Sample
- {
- public var child:Sample;
- public var data1:Number;
- public var data2:Number;
- public var data3:Number;
- public function Sample(data1:Number, data2:Number, data3:Number)
- {
- this.data1 = data1;
- this.data2 = data2;
- this.data3 = data3;
- }
- }
[最適化 Tips] 変数名の長さによる処理速度の違い forked from: [最適化 Tips] 変数名の長さによる処理速度の違い
- // forked from muta244's [最適化 Tips] 変数名の長さによる処理速度の違い
- package {
- import flash.display.*;
- import flash.events.*;
- import flash.text.*;
- import flash.utils.*;
- public class Main extends Sprite
- {
- static private const _NUM_TIMES:uint = 1000000;
- private var ary:Array = [
- 1, 0, 0, 0, 0,
- 0, 1, 0, 0, 0,
- 0, 0, 1, 0, 0,
- 0, 0, 0, 1, 0
- ];
- private var ary2:Array = [
- 1, 0, 0, 0, 255,
- 0, 1, 0, 0, 255,
- 0, 0, 1, 0, 255,
- 0, 0, 0, 1, 0
- ];
- private function _init():void
- {
- _debug(
- "各テスト " + _NUM_TIMES + " 回処理させた計算結果 [単位 : ミリ秒]\n" +
- "(誤差は多少生じます)\n"
- );
- _measure("ループのみ", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- }
- });
- _measure("配列の要素を1つ変更", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- ary[4] = 255;
- }
- });
- _measure("配列の要素を2つ変更", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- ary[4] = 255;
- ary[9] = 255;
- }
- });
- _measure("配列の要素を3つ変更", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- ary[4] = 255;
- ary[9] = 255;
- ary[14] = 255;
- }
- });
- _measure("配列の要素を全て変更", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- ary[0] = 1;
- ary[1] = 0;
- ary[2] = 0;
- ary[3] = 0;
- ary[4] = 255;
- ary[5] = 0;
- ary[6] = 1;
- ary[7] = 0;
- ary[8] = 0;
- ary[9] = 255;
- ary[10] = 0;
- ary[11] = 0;
- ary[12] = 1;
- ary[13] = 0;
- ary[14] = 255;
- ary[15] = 0;
- ary[16] = 0;
- ary[17] = 0;
- ary[18] = 1;
- ary[19] = 0;
- }
- });
- _measure("事前に作成した配列の代入", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- ary = ary2;
- }
- });
- _measure("新しく配列を作成する([])", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- ary = [
- 1, 0, 0, 0, 255,
- 0, 1, 0, 0, 255,
- 0, 0, 1, 0, 255,
- 0, 0, 0, 1, 0
- ];
- }
- });
- _measure("新しく配列を作成する(new Array())", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- ary = new Array(
- 1, 0, 0, 0, 255,
- 0, 1, 0, 0, 255,
- 0, 0, 1, 0, 255,
- 0, 0, 0, 1, 0
- );
- }
- });
- _debug("\n結果については言及しませんので, 各自ご判断ください.");
- }
- private var _field:TextField;
- private var _time:uint;
- public function Main():void
- {
- _setup();
- _init();
- }
- private function _measure(title:String, func:Function, ...params):void
- {
- _time = getTimer();
- func.apply(null, params);
- _time = getTimer() - _time;
- _debug("[ " + title + " ] --> " + _time + " ms");
- }
- private function _debug(log:String):void
- {
- _field.appendText(log + "\n");
- }
- private function _setup():void
- {
- _field = new TextField();
- _field.width = stage.stageWidth - 40;
- _field.height = stage.stageHeight - 60;
- _field.x = 20;
- _field.y = 60;
- var format:TextFormat = _field.defaultTextFormat;
- format.font = "_sans";
- _field.defaultTextFormat = format;
- addChild(_field);
- var button:Sprite = new Sprite();
- button.graphics.lineStyle(1, 0xBBBBBB);
- button.graphics.beginFill(0xEEEEEE);
- button.graphics.drawRoundRect(0, 0, 100, 20, 5, 5);
- button.graphics.endFill();
- addChild(button);
- button.x = 20;
- button.y = 20;
- button.mouseChildren = false;
- button.buttonMode = true;
- var field:TextField = new TextField();
- field.width = 100;
- field.height = 20;
- field.htmlText = "<p align='center'><font face='_sans'>再計算</span></p>";
- button.addChild(field);
- button.addEventListener(MouseEvent.CLICK, function ():void
- {
- _field.text = "";
- _init();
- });
- }
- }
- }
[最適化 Tips] 変数名の長さによる処理速度の違い [最適化 Tips] SpriteとMovieClipってそんなに変わるの?
- // forked from muta244's [最適化 Tips] 変数名の長さによる処理速度の違い
- package {
- import flash.display.*;
- import flash.events.*;
- import flash.text.*;
- import flash.utils.*;
- import flash.system.*;
- public class Main extends Sprite
- {
- static private const _NUM_TIMES:uint = 5000;
- private var wrap:Sprite;
- private function _init():void
- {
- while(wrap.numChildren) wrap.removeChildAt(0);
- _debug(
- "各テスト " + _NUM_TIMES + " 回処理させた計算結果 [単位 : ミリ秒]\n" +
- "(誤差は多少生じます)\n"
- );
- var old:int = System.totalMemory;
- _measure("Sprite", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- var sp:Sprite = new Sprite();
- wrap.addChild(sp)
- }
- });
- _debug("(Spriteのメモリ使用量 : " + int((System.totalMemory - old)/1000) + "KB");
- while(wrap.numChildren) wrap.removeChildAt(0);
- System.gc();
- _debug("")
- var old1:int = System.totalMemory;
- _measure("MovieClip", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- var mc:MovieClip = new MovieClip();
- wrap.addChild(mc)
- }
- });
- _debug("(MovieClipのメモリ使用量 : " + int((System.totalMemory - old1)/1000) + "KB");
- _debug("\n結果については言及しませんので, 各自ご判断ください.");
- }
- private var _field:TextField;
- private var _time:uint;
- public function Main():void
- {
- _setup();
- _init();
- }
- private function _measure(title:String, func:Function, ...params):void
- {
- _time = getTimer();
- func.apply(null, params);
- _time = getTimer() - _time;
- _debug("[ " + title + " ] --> " + _time + " ms");
- }
- private function _debug(log:String):void
- {
- _field.appendText(log + "\n");
- }
- private function _setup():void
- {
- wrap = new Sprite
- addChild(wrap)
- _field = new TextField();
- _field.width = stage.stageWidth - 40;
- _field.height = stage.stageHeight - 60;
- _field.x = 20;
- _field.y = 60;
- var format:TextFormat = _field.defaultTextFormat;
- format.font = "_sans";
- _field.defaultTextFormat = format;
- addChild(_field);
- var button:Sprite = new Sprite();
- button.graphics.lineStyle(1, 0xBBBBBB);
- button.graphics.beginFill(0xEEEEEE);
- button.graphics.drawRoundRect(0, 0, 100, 20, 5, 5);
- button.graphics.endFill();
- addChild(button);
- button.x = 20;
- button.y = 20;
- button.mouseChildren = false;
- button.buttonMode = true;
- var field:TextField = new TextField();
- field.width = 100;
- field.height = 20;
- field.htmlText = "<p align='center'><font face='_sans'>再計算</span></p>";
- button.addChild(field);
- button.addEventListener(MouseEvent.CLICK, function ():void
- {
- _field.text = "";
- _init();
- });
- }
- }
- }
[最適化 Tips] 変数名の長さによる処理速度の違い forked from: [最適化 Tips] Faster Math.sqrt?
- // forked from muta244's [最適化 Tips] 変数名の長さによる処理速度の違い
- package {
- import flash.display.*;
- import flash.events.*;
- import flash.text.*;
- import flash.utils.*;
- public class Main extends Sprite
- {
- static private const _NUM_TIMES:uint = 1000000;
- private var _someNumber:int = Math.random() * 10000;
- private function _init():void
- {
- _debug(
- "Math.sqrt vs custom function " + _NUM_TIMES + " cycles.\n"
- );
- _measure("Empty loop", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- }
- });
- _measure("Number reference time", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- _someNumber;
- }
- });
- _measure("Math.sqrt", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- Math.sqrt(_someNumber);
- }
- });
- _measure("Fast SquareRoot FunctionCall", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- FSQRT(_someNumber)
- }
- });
- _measure("Fast SquareRoot Inline", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- var thres:Number = 0.004;
- var b:Number = _someNumber * 0.25;
- var a:Number;
- var c:Number;
- do
- {
- c = _someNumber/ b;
- b = (b + c) * 0.5;
- a = b - c;
- if (a < 0) a = -a;
- }
- while (a > thres);
- }
- });
- _debug("\n Either via function call or inline, FastSquareRoot loses");
- }
- private var _field:TextField;
- private var _time:uint;
- public function Main():void
- {
- _setup();
- _init();
- }
- private function _measure(title:String, func:Function, ...params):void
- {
- _time = getTimer();
- func.apply(null, params);
- _time = getTimer() - _time;
- _debug("[ " + title + " ] --> " + _time + " ms");
- }
- private function _debug(log:String):void
- {
- _field.appendText(log + "\n");
- }
- private function _setup():void
- {
- _field = new TextField();
- _field.width = stage.stageWidth - 40;
- _field.height = stage.stageHeight - 60;
- _field.x = 20;
- _field.y = 60;
- var format:TextFormat = _field.defaultTextFormat;
- format.font = "_sans";
- _field.defaultTextFormat = format;
- addChild(_field);
- var button:Sprite = new Sprite();
- button.graphics.lineStyle(1, 0xBBBBBB);
- button.graphics.beginFill(0xEEEEEE);
- button.graphics.drawRoundRect(0, 0, 100, 20, 5, 5);
- button.graphics.endFill();
- addChild(button);
- button.x = 20;
- button.y = 20;
- button.mouseChildren = false;
- button.buttonMode = true;
- var field:TextField = new TextField();
- field.width = 100;
- field.height = 20;
- field.htmlText = "<p align='center'><font face='_sans'>Test Again</span></p>";
- button.addChild(field);
- button.addEventListener(MouseEvent.CLICK, function ():void
- {
- _field.text = "";
- _init();
- });
- }
- public static function FSQRT(val:Number):Number
- {
- var thres:Number = 0.002;
- var b:Number = val * 0.25;
- var a:Number;
- var c:Number;
- do
- {
- c = val / b;
- b = (b + c) * 0.5;
- a = b - c;
- if (a < 0) a = -a;
- }
- while (a > thres);
- return b;
- }
- }
- }
[最適化 Tips] 変数名の長さによる処理速度の違い [最適化 Tips] if ... else と switch での処理速度の違い
- package {
- import flash.display.*;
- import flash.events.*;
- import flash.text.*;
- import flash.utils.*;
- public class Main extends Sprite
- {
- static private const _NUM_TIMES:uint = 1000000;
- private function _init():void
- {
- _debug(
- "各テスト " + _NUM_TIMES + " 回処理させた計算結果 [単位 : ミリ秒]\n" +
- "(誤差は多少生じます)\n"
- );
- var n:uint = 10;
- _measure("ループのみ", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- }
- });
- _measure("1 回のみ比較する if ... else 文", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- if (n === 0) {}
- else {}
- }
- });
- _measure("1 回のみ比較する switch 文", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- switch (n) {
- case 0: break;
- default: break;
- }
- }
- });
- _measure("10 回比較する if ... else 文", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- if (n === 0) {}
- else if (n === 1) {}
- else if (n === 2) {}
- else if (n === 3) {}
- else if (n === 4) {}
- else if (n === 5) {}
- else if (n === 6) {}
- else if (n === 7) {}
- else if (n === 8) {}
- else if (n === 9) {}
- else {}
- }
- });
- _measure("10 回比較する switch 文", function ():void
- {
- for (var i:uint = 0; i < _NUM_TIMES; i++) {
- switch (n) {
- case 0: break;
- case 1: break;
- case 2: break;
- case 3: break;
- case 4: break;
- case 5: break;
- case 6: break;
- case 7: break;
- case 8: break;
- case 9: break;
- default: break;
- }
- }
- });
- _debug("\n結果については言及しませんので, 各自ご判断ください.");
- }
- private var _field:TextField;
- private var _time:uint;
- public function Main():void
- {
- _setup();
- _init();
- }
- private function _measure(title:String, func:Function, ...params):void
- {
- _time = getTimer();
- func.apply(null, params);
- _time = getTimer() - _time;
- _debug("[ " + title + " ] --> " + _time + " ms");
- }
- private function _debug(log:String):void
- {
- _field.appendText(log + "\n");
- }
- private function _setup():void
- {
- _field = new TextField();
- _field.width = stage.stageWidth - 40;
- _field.height = stage.stageHeight - 60;
- _field.x = 20;
- _field.y = 60;
- _field.multiline = true;
- _field.wordWrap = true;
- var format:TextFormat = _field.defaultTextFormat;
- format.font = "_sans";
- _field.defaultTextFormat = format;
- addChild(_field);
- var button:Sprite = new Sprite();
- button.graphics.lineStyle(1, 0xBBBBBB);
- button.graphics.beginFill(0xEEEEEE);
- button.graphics.drawRoundRect(0, 0, 100, 20, 5, 5);
- button.graphics.endFill();
- addChild(button);
- button.x = 20;
- button.y = 20;
- button.mouseChildren = false;
- button.buttonMode = true;
- var field:TextField = new TextField();
- field.width = 100;
- field.height = 20;
- field.htmlText = "<p align='center'><font face='_sans'>再計算</span></p>";
- button.addChild(field);
- button.addEventListener(MouseEvent.CLICK, function ():void
- {
- _field.text = "";
- _init();
- });
- }
- }
- }
notice:





