In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Matrix3D初期化速度

new、identity、rawDataでの比較。あくまでも参考値
Get Adobe Flash player
by zahir 01 Nov 2010
/**
 * Copyright zahir ( http://wonderfl.net/user/zahir )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dP08
 */

package {
    import flash.events.MouseEvent;
    import flash.geom.Matrix3D;
    import flash.text.TextField;
    import flash.display.Sprite;
    import flash.utils.getTimer;
    public class FlashTest extends Sprite {
        private var t:TextField;
        public function FlashTest() {
            // write as3 code here..
            t= new TextField();
            addChild(t).width = t.height = 465;
            
            addEventListener( MouseEvent.CLICK, onClick );
            
            ex();
        }
        
        private function onClick( e:MouseEvent ):void
        {
            ex();
        }

        private function ex():void
        {
            var m:Matrix3D;
            var _m:Matrix3D = new Matrix3D();
            var v:Vector.<Number> = new Matrix3D().rawData;
            var i:int = 0;                                      
            var len:int = 1000000;
            var time1:int;
            var time2:int;
            var recode:Array = [];
            var count:int = 0;
            t.text = "";
            
            time1 = getTimer();
            for( i = 0; i<len; i++ )
            {
                m = new Matrix3D();
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "new", time2 );
            
            time1 = getTimer();
            for( i = 0; i<len; i++ )
            {
                m.identity();
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "identity", time2 );
            
            time1 = getTimer();
            for( i = 0; i<len; i++ )
            {
                m.rawData = v;
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "rawData", time2 );
            
            time1 = getTimer();
            for( i = 0; i<len; i++ )
            {
                m.rawData = MyStatic.MATRIX3D_IDENTITY_RAWDATA;
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "static rawData", time2 );
            
            time1 = getTimer();
            for( i = 0; i<len; i++ )
            {
                m = _m.clone();
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "clone", time2 );
            
            /*お話にならないほど低速
            time1 = getTimer();
            for( i = 0; i<len; i++ )
            {
                m.rawData = new <Number>[1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1];
            }
            time2 = getTimer() - time1;
            recode[count++] = new TimeData( "rawData = new Vector", time2 );
            //*/
            
            recode.sortOn( "time", Array.NUMERIC );
            var max:int = (recode[ recode.length - 1 ] as TimeData).time;
            for( i = 0, len = recode.length; i<len; i++)
            {
                var data:TimeData = recode[i] as TimeData;
                var n:int = max / data.time * 100;
                t.appendText( data.title + " :: " + data.time + " ms\n\t\t最低速に比べて約 " + n + " %高速 \n\n");
            }
            
            t.appendText("clickで再計算");
        }

    }
}
import flash.geom.Matrix3D;
class TimeData
{
    public var title:String;
    public var time:int;
    public function TimeData( title:String = "", time:int = 0 )
    {
        this.title = title;
        this.time = time;
    }

}

class MyStatic
{
    public static const MATRIX3D_IDENTITY_RAWDATA:Vector.<Number> = new Matrix3D().rawData;
}