cast speed

by CoremindJP
♥0 | Line 153 | Modified 2009-10-11 21:49:30 | MIT License
play

ActionScript3 source code

/**
 * Copyright CoremindJP ( http://wonderfl.net/user/CoremindJP )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/M02q
 */

package
{
    import flash.utils.getTimer;
    import flash.text.TextField;
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    
    public class FlashTest extends Sprite
    {
        private var
            tf:TextField,
            time:int,
            array:Array,
            vector:Vector.<Cast>;
        
        public function FlashTest()
        {
            var
                i:int = 0,
                _length:int = 10000,
                _instanceBuffer:Cast;
                
            tf = new TextField();
            this.addChild( tf );
            tf.width = tf.height = 465;
            tf.text =  _length + "ループ計算中"
            
    //要素追加処理速度
        //Array
            //pushで追加
            tf.text = "<SET><ARRAY> push = ";
            array = [];
            time = getTimer();
            for( i = 0; i < _length; i++ )
               array.push( new Cast() );
            tf.appendText( String( getTimer() - time ) + "\n" );
            
            //lengthをインデックスにして追加
            tf.appendText( "<SET><ARRAY> length = " );
            array = [];
            time = getTimer();
            for( i = 0; i < _length; i++ )
               array[array.length] = new Cast();
            tf.appendText( String( getTimer() - time ) + "\n" );
            
            //ループidをインデックスにして追加
            tf.appendText( "<SET><ARRAY> loopID = " );
            array = [];
            time = getTimer();
            for( i = 0; i < _length; i++ )
               array[i] = new Cast();
            tf.appendText( String( getTimer() - time ) + "\n" );
            
        //Vector
            //pushで追加
            tf.appendText( "<SET><Vector> push = " );
            vector = Vector.<Cast>([]);
            time = getTimer();
            for( i = 0; i < _length; i++ )
               vector.push( new Cast() );
            tf.appendText( String( getTimer() - time ) + "\n" );
            
            //lengthをインデックスにして追加
            tf.appendText( "<SET><Vector> length = " );
            vector = Vector.<Cast>([]);
            time = getTimer();
            for( i = 0; i < _length; i++ )
               vector[vector.length] = new Cast();
            tf.appendText( String( getTimer() - time ) + "\n" );
            
            //ループidをインデックスにして追加
            tf.appendText( "<SET><Vector> loopID = " );
            vector = Vector.<Cast>([]);
            time = getTimer();
            for( i = 0; i < _length; i++ )
               vector[i] = new Cast();
            tf.appendText( String( getTimer() - time ) + "\n" );            
            
    //要素取得処理
        //Array
            //直で操作
            tf.appendText( "<GET><ARRAY> array = " );
            time = getTimer();
            for( i = 0; i < _length; i++ )
            {
                array[i].a = "KAKIKAE";
                array[i].b = [ 40, "test", 53.64, new Shape() ];
                array[i].d.bitmapData = new BitmapData( 50, 50, true );
                array[i].e = -4.5289;
            }
            tf.appendText( String( getTimer() - time ) + "\n" );
            
            //cast型の変数に代入してから操作
            tf.appendText( "<GET><ARRAY> VariableCast = " );
            _instanceBuffer = null;
            time = getTimer();
            for( i = 0; i < _length; i++ )
            {
                _instanceBuffer = array[i];
                _instanceBuffer.a = "KAKIKAE";
                _instanceBuffer.b = [ 40, "test", 53.64, new Shape() ];
                _instanceBuffer.d.bitmapData = new BitmapData( 50, 50, true );
                _instanceBuffer.e = -4.5289;
            }
            tf.appendText( String( getTimer() - time ) + "\n" );
            
            //asでキャストした後操作
            tf.appendText( "<GET><ARRAY> as Cast = " );
            _instanceBuffer = null;
            time = getTimer();
            for( i = 0; i < _length; i++ )
            {
                _instanceBuffer = array[i] as Cast;
                _instanceBuffer.a = "KAKIKAE";
                _instanceBuffer.b = [ 40, "test", 53.64, new Shape() ];
                _instanceBuffer.d.bitmapData = new BitmapData( 50, 50, true );
                _instanceBuffer.e = -4.5289;
            }
            tf.appendText( String( getTimer() - time ) + "\n" );
            
        //Vector
            //直で操作
            tf.appendText( "<GET><Vector> vector = " );
            time = getTimer();
            for( i = 0; i < _length; i++ )
            {
                vector[i].a = "KAKIKAE";
                vector[i].b = [ 40, "test", 53.64, new Shape() ];
                vector[i].d.bitmapData = new BitmapData( 50, 50, true );
                vector[i].e = -4.5289;
            }
            tf.appendText( String( getTimer() - time ) + "\n" );
            
            //cast型の変数に代入してから操作
            tf.appendText( "<GET><Vector> VariableCast = " );
            _instanceBuffer = null;
            time = getTimer();
            for( i = 0; i < _length; i++ )
            {
                _instanceBuffer = vector[i];
                _instanceBuffer.a = "KAKIKAE";
                _instanceBuffer.b = [ 40, "test", 53.64, new Shape() ];
                _instanceBuffer.d.bitmapData = new BitmapData( 50, 50, true );
                _instanceBuffer.e = -4.5289;
            }
            tf.appendText( String( getTimer() - time ) + "\n" );
            
            //asでキャストした後操作
            tf.appendText( "<GET><Vector> as Cast = " );
            _instanceBuffer = null;
            time = getTimer();
            for( i = 0; i < _length; i++ )
            {
                _instanceBuffer = vector[i] as Cast;
                _instanceBuffer.a = "KAKIKAE";
                _instanceBuffer.b = [ 40, "test", 53.64, new Shape() ];
                _instanceBuffer.d.bitmapData = new BitmapData( 50, 50, true );
                _instanceBuffer.e = -4.5289;
            }
            tf.appendText( String( getTimer() - time ) + "\n" );            
        }
    }
    
}

import flash.display.Sprite;
import flash.display.Bitmap;

class Cast extends Sprite
{
    public var
        a:String,
        b:Array,
        c:int,
        d:Bitmap,
        e:Number;
        
    public function Cast()
    {
        a = "castTest";
        b = [];
        for( var i:int = 0; i < 100; i++ )
            b[i] = i * 40;
            
        c = 99;
        d = new Bitmap();
        e = 5.26849;
    }
}    

Forked