XML Attribute access performance test

by leichtgewicht
♥0 | Line 51 | Modified 2012-02-26 18:13:55 | 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/hPxg
 */

package {
    import flash.text.TextField;
    import flash.utils.getTimer;
    import flash.display.Sprite;
    
    public class FlashTest extends Sprite {
        
        private const tf: TextField = new TextField;
        
        public function FlashTest() {
            
            addChild( tf );
            tf.width = stage.stageWidth;
            tf.height = stage.stageHeight;
            
            var t: Number;
            const c: int = 750000;
            var i: int;
            var xml: XML = <xml dynamic="true"/>
            t = getTimer();
            for( i=0; i<c; ++i ) {
                xml.@dynamic;
            }
            t = getTimer() - t;
            trace("e4x access: " + t + "ms");
            t = getTimer();
            for( i=0; i<c; ++i ) {
                xml.attribute('dynamic');
            }
            t = getTimer() - t;
            trace("attribute access: " + t + "ms");
            t = getTimer();
            for( i=0; i<c; ++i ) {
                xml.attributes.dynamic;
            }
            t = getTimer() - t;
            trace("attributes access: " + t + "ms");
            t = getTimer();
            for( i=0; i<c; ++i ) {
                xml['@dynamic'];
            }
            t = getTimer() - t;
            trace("dynamic access: " + t + "ms");
            var q: QName = new QName(null, "@dynamic");
            t = getTimer();
            for( i=0; i<c; ++i ) {
                xml[q];
            }
            t = getTimer() - t;
            trace("qname access: " + t + "ms");
        }
        
        private function trace(text: String):void {
            tf.appendText(text+"\n");
        }

    }
}