ActionScript 3 - First Steps XIII

      JAVA 2005-10-20 21:46

Ok, now for something real - XML. At the labs site you can read about E4X, the integration of XML in Actionscript. Gordon Smith gives a nice overview of the new possibilities. I liked this part most: "Compared with the current XML support in the Flash Player, E4X allows you to write less code and execute it faster because more processing can be done at the native speed of C++." Wohoo, the native speed of C++, that's definitely a nice way to go. So let's do a speed test.
package {

	import flash.display.Sprite;
	
	import flash.util.trace;
	import flash.util.getTimer;
	
	import flash.xml.XMLNode;
	import flash.xml.XMLDocument;


	public class Test extends Sprite {
		
		public function Test() {
			var employees:XML =
		        <employees>
		            <employee ssn="123-123-1234">
		                <name first="John" last="Doe"/>
		                <address>
		                    <street>11 Main St.</street>
		                    <city>San Francisco</city>
		                    <state>CA</state>
		                    <zip>98765</zip>
		                </address>
		            </employee>
		            <employee ssn="789-789-7890">
		                <name first="Mary" last="Roe"/>
		                <address>
		                    <street>99 Broad St.</street>
		                    <city>Newton</city>
		                    <state>MA</state>
		                    <zip>01234</zip>
		                </address>
		            </employee>
		        </employees>;
		        
		    	var employees2:XMLDocument = new XMLDocument('<employees><employee 
ssn="123-123-1234"><name first="John" 
last="Doe"/><address><street>11 Main 
St.</street><city>San 
Francisco</city><state>CA</state><
zip>98765</zip></address></employee><employee
 ssn="789-789-7890"><name first="Mary" 
last="Roe"/><address><street>99 Broad St.</street><city>Newton</city><
state>MA</state><zip>01234</zip>
</address></employee></employees>');    


		        var t:int = getTimer();
		        for( var i:int=0; i<10000;i++){
		        	var x:XMLList = employees.employee.(@ssn == "789-789-7890");
		        }
		        trace( getTimer() - t);
		        
		        var t:int = getTimer();
		        for( var i:int=0; i<10000;i++){
		        	var n:XMLNode = employees2.firstChild.firstChild;
		        	
		        	var nFound:Array = new Array();
		        	while( n != null){
		        		if( n.attributes.ssn == "789-789-7890"){
		        			nFound.push( n);
		        		}
		        		n = n.nextSibling;
		        	}
		        	
		        }
		        trace( getTimer() - t);
		}
		
	}
	
}



Results:
268
23

Huh!? Unless i'm missing something substantial, E4X seems to be more than ten times slower than the good old XML object (which is now named XMLDocument). Probably this is because of the alpha state of the player. It will be very interesting to see, how fast the final E4X is.
Nevertheless the new way of accessing and filtering XML is really great in contrast to the DOM. Also embedding XML in the source code is much easier now. It leaves me wondering, why strings still have to be terminated on the same line they started. Wouldn't it be great, if we could spread strings accross lines too?

var s:String = "hi
i'm a string spread accross
multiple lines";


Cheers,
Ralf.
////////////////////////////////////////
因为是个人读书的笔记,所以没有进行翻译,只是对其中关键地方进行注释。如有错误欢迎指出。
原文转载自: www.helpqlodhelp.com/blog/
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}