Hello flexcoders;

 

Has anyone else experienced terrible performance building nested XML trees?

 

I wrote a test ActionScript app that builds a nested XML document with the structure:

 

<or>

            <t/>

            <or>

                        <t/>

                        <or>

                                    <t/>

                                    <t/>

            </or>

</or>

 

I wrote two functions to generate the XML. One using recursion (ToDeepXML), and one using only iteration (ToDeepXMLNoRecurse). (I also wrote ToWideXML which builds a balanced tree). The timing results for 200 terms are as follows:

 

ToWideXml 343 ms

ToDeepXml 434213 ms

ToDeepXmlNoRecurs 31 ms

 

There is a performance penalty to paid for using recursion, but I don’t think this could account for an exponential difference in performance, like what we’re seeing here. If XML object are represented internally as strings, then the performance difference could be explained by the fact that the recursive algorithm does insertions, while the non-recursive algorithm only ever appends. The insertions would involve a lot memory copying, which never helps performance.

 

Here’s the code I used for testing:

 

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute"

            creationComplete="OnCreationComplete()">

            <mx:Script>

                        <![CDATA[

                                    import mx.utils.ObjectUtil;

                                    private function OnCreationComplete() : void

                                    {

                                                var a : Array = [];

                                                for( var i : uint = 0; i < 200; i++ )

                                                {

                                                            a.push( uint( Math.random() * 10000 ) );                                                  

                                                }

                                               

                                                var b : Array = ObjectUtil.copy( a ) as Array;

                                                var c : Array = ObjectUtil.copy( a ) as Array

                                               

                                                Time( function() : void{ ToWideXml( a ) }, "ToWideXml" );                                       

                                                Time( function() : void{ ToDeepXml( b ) }, "ToDeepXml" );                                       

                                                Time( function() : void{ ToDeepXmlNoRecurs( c ) }, "ToDeepXmlNoRecurs" );

                                    }

                                   

                                    private function ToDeepXml( a : Array ) : XML

                                    {

                                                if( a.length == 1 )

                                                {

                                                            return <t>{a[0]}</t>;

                                                }

                                               

                                                var n : Number = Number( a.pop() );

                                               

                                                return <or><t>{n}</t>{ToDeepXml( a )}</or>;                                

                                    }

                                   

                                    private function ToDeepXmlNoRecurs( a : Array ) : XML

                                    {                                              

                                                var result : XML = <or/>;

                                                var curNode: XML = result;

                                               

                                                for( var i : uint = 0; i < a.length - 2; i++ )

                                                {

                                                            curNode.appendChild( <t>{a[ i ]}</t> );                                                                

                                                            curNode.appendChild( <or/> );                                                    

                                                            curNode = curNode.children()[ 1 ];

                                                }

                                               

                                                curNode.appendChild( <t>{ a[ a.length - 1 ] }</t> );

                                                curNode.appendChild( <t>{ a[ a.length ] }</t> );

                                               

                                                return result;

                                    }

 

                                    private function ToWideXml( a : Array ) : XML

                                    {

                                                if( a.length == 1 )

                                                {

                                                            return <t>{a[0]}</t>;

                                                }

                                               

                                                var a1 : Array = a.splice( 0, a.length / 2 );

                                               

                                                return <or><t>{ToWideXml( a1 )}</t><t>{ToWideXml( a )}</t></or>;

                                    }

                                   

                                    private function Time( f : Function, msg : String = "") : void

                                    {

                                                var t : uint = new Date().time;

                                                f();

                                                var t1 : uint = new Date().time;

                                               

                                                trace( msg, t1 - t, "ms" );

                                    }

                        ]]>

            </mx:Script>

</mx:Application>

Kodak Graphic Communications Canada Company

Tobias Patton | Software Developer | Tel: +1.604.451.2700 ext: 5148 | mailto:[EMAIL PROTECTED] | http://www.creo.com

 



--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com




SPONSORED LINKS
Web site design development Computer software development Software design and development
Macromedia flex Software development best practice


YAHOO! GROUPS LINKS




Reply via email to