forked from: Type casting comparison

by kinoppy forked from Type casting comparison (diff: 1)
♥0 | Line 53 | Modified 2010-12-09 14:41:56 | MIT License
play

ActionScript3 source code

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

// forked from Fumio's Type casting comparison
package {
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.utils.getTimer;
    [SWF(width = "240",height = "180")]
    public class TypeCasting extends Sprite {
        private const COUNT:int = 10000000;
        private var _mc:Object = new MovieClip();
        private var started:uint;
        private var my_txt:TextField = new TextField();
        private var label_txt:TextField = new TextField();
        public function TypeCasting() {
            // Creating a TextField for display
            createTextField();
            // Starting test
            cast();
            as_operator();
            untyped();
        }
        private function cast():void {
            started = getTimer();
            for (var i:int = 0; i < COUNT; i++) {
                var test:MovieClip = MovieClip(_mc);
            }
            xTrace(getTimer() - started);
        }
        private function as_operator():void {
            started = getTimer();
            for (var i:int = 0; i < COUNT; i++) {
                var test:MovieClip = _mc as MovieClip;
            }
            xTrace(getTimer() - started);
        }
        private function untyped():void {
            started = getTimer();
            for (var i:int = 0; i < COUNT; i++) {
                var temp:* = _mc;
                var test:MovieClip = temp;
            }
            xTrace(getTimer() - started);
        }
        private function createTextField():void {
            addChild(my_txt);
            addChild(label_txt);
            my_txt.autoSize = TextFieldAutoSize.RIGHT;
            label_txt.autoSize = TextFieldAutoSize.LEFT;
            label_txt.text = "cast:\nas operator:\nuntyped:";
        }
        private function xTrace(n:int):void {
            my_txt.appendText(String(n) + "\n");
        }
    }
}