Performance Test, Constructor versus init

by leichtgewicht
Very rudimentary tests to evaluate when/if a constructor really is faster...
♥0 | Line 185 | Modified 2011-09-08 13:12:38 | MIT License
play

ActionScript3 source code

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

/**
 * After seeing many people using a "init" function to construct their classes I felt compelled to see how fast it really is.
 *
 * I used simple statements in that example, trying to see if the results change significantly related to the content.
 */
package {
    import flash.text.TextField;
    import flash.display.Sprite;
    import flash.utils.setTimeout;
    import flash.utils.getTimer;
    
    public class FlashTest extends Sprite {
        private var _text: TextField = new TextField();
        public function FlashTest() {
            setTimeout(start, 80);
            addChild(_text);
            _text.width = stage.stageWidth;
            _text.height = stage.stageHeight;
        }
        public function start():void {
            const amount: int = 400000;
            run(OneProperty,amount);
            run(OnePropertyInit,amount);
            run(TwoProperty,amount);
            run(TwoPropertyInit,amount);
            run(ThreeProperty,amount);
            run(ThreePropertyInit,amount);
            run(FourProperty,amount);
            run(FourPropertyInit,amount);
            run(Iterating,amount);
            run(IteratingInit,amount);
            run(Complex,amount);
            run(ComplexInit,amount);
        }
        
        private function run(clazz:Class, amount:int):void {
            var t: Number;
            var i: int;
            t = getTimer();
            for( i = amount; i>0; --i ) {
                new clazz();
            }
            t = getTimer()-t;
            _text.appendText(clazz+": "+t+"ms\n");
        }

    }
}

class OneProperty {
    
    private var _a: String;
    
    function OneProperty(a:String=null) {
        _a = a;
    }
}

class OnePropertyInit {
    
    private var _a: String;
    
    function OnePropertyInit(a:String=null) {
        init(a);
    }
    
    private function init(a:String=null):void {
        _a = a;
    }

}

class TwoProperty {
    
    private var _a: String;
    private var _b: String;
    
    function TwoProperty(a:String=null,b:String=null) {
        _a = a;
        _b = b;
    }
}

class TwoPropertyInit {
    
    private var _a: String;
    private var _b: String;
    
    function TwoPropertyInit(a:String=null,b:String=null) {
        init(a,b);
    }
    
    private function init(a:String,b:String):void {
        _a = a;
        _b = b;
    }

}
class ThreeProperty {
    
    private var _a: String;
    private var _b: String;
    private var _c: String;
    
    function ThreeProperty(a:String=null,b:String=null,c:String=null) {
        _a = a;
        _b = b;
        _c = c;
    }
}

class ThreePropertyInit {
    
    private var _a: String;
    private var _b: String;
    private var _c: String;
    
    function ThreePropertyInit(a:String=null,b:String=null,c:String=null) {
        init(a,b,c);
    }
    
    private function init(a:String,b:String,c:String):void {
        _a = a;
        _b = b;
        _c = c;
    }
}
class FourProperty {
    
    private var _a: String;
    private var _b: String;
    private var _c: String;
    private var _d: String;
    
    function FourProperty(a:String=null,b:String=null,c:String=null,d:String=null) {
        _a = a;
        _b = b;
        _c = c;
        _d = d;
    }
}

class FourPropertyInit {
    
    private var _a: String;
    private var _b: String;
    private var _c: String;
    private var _d: String;
    
    function FourPropertyInit(a:String=null,b:String=null,c:String=null,d:String=null) {
        init(a,b,c,d);
    }
    
    private function init(a:String,b:String,c:String,d:String):void {
        _a = a;
        _b = b;
        _c = c;
        _d = d;
    }
}
const array: Array = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
class Iterating {
    
    function Iterating() {
       var a: Number;
       for each( var key:* in array ) {
           a = key;
       }
    }
}


class IteratingInit {
    
    function IteratingInit() {
        init();
    }
    
    private function init():void {
       var a: Number;
       for each( var key:* in array ) {
           a = key;
       }
    }
}

class Complex {
    
    private static function statik():void {
    }

    
    public function Complex(b:int = 0) {
         // Lots of optiomizable code
         var a: String = "2"+b;
         for each( var key:* in array) {}
         var c: String = "3"+"2"+int(0);
         statik();
         while( false ) {
             statik();
         }
         priv();
         while( false ) {
             priv();
         }

    }

    private function priv():void {
    }
}


class ComplexInit {
    
    private static function statik():void {
    }

    
    public function ComplexInit(b:int = 0) {
        init(b);
    }
    
    private function init(b:int):void {
         var a: String = "2"+b;
         for each( var key:* in array) {}
         var c: String = "3"+"2"+int(0);
         statik();
         while( false ) {
             statik();
         }
         priv();
         while( false ) {
             priv();
         }
    }

    private function priv():void {
    }
}