<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import com.iwobanas.utils.StringBuffer;
            
            [Bindable]
            public var generationTime:Number = 0;
            
            [Bindable]
            public var concatenationTime:Number = 0;
            
            [Bindable]
            public var bufferTime:Number = 0;
            
            [Bindable]
            protected var dataArray:Array;
            
            protected function generate():void
            {
                var start:Number = new Date().time;
                var nr:Number = numItemsCombo.selectedItem as Number;
                dataArray = new Array(nr);
                
                for (var i:Number = 0; i < nr; i++)
                {
                    dataArray[i] = "str" + Math.random();
                }
                var end:Number = new Date().time;
                generationTime = end - start;
            }
            
            protected function concatenateTest():void
            {
                var result:String;
                var start:Number = new Date().time;
                result = "";
                for each (var item:String in dataArray)
                {
                    result += item + "\n";
                }
                var end:Number = new Date().time;
                concatenationTime = end - start;
                trace("concatenate - length:", result.length, "time:", concatenationTime);
            }
            
            protected function bufferTest():void
            {
                var result:String;
                var start:Number = new Date().time;
                var buffer:StringBuffer = new StringBuffer();
                for each (var item:String in dataArray)
                {
                    buffer.add(item);
                    buffer.add("\n");
                }
                result = buffer.toString();
                var end:Number = new Date().time;
                bufferTime = end - start;
                trace("buffer - length:", result.length, "time:", bufferTime);
            }
            
            protected function resetAll():void
            {
                dataArray = null;
                generationTime = 0;
                concatenationTime = 0;
                bufferTime = 0;
            }
        ]]>
    </mx:Script>
    <mx:ComboBox id="numItemsCombo" dataProvider="{[10000,100000,1000000]}" change="resetAll()" />
    
    <mx:Button label="Generate Data" click="generate()" />
    <mx:Label text="Generation time: {generationTime} ms" />
    
    <mx:Button label="Standard Concatenation" enabled="{dataArray!=null}" click="concatenateTest()" />
    <mx:Label text="Concatenation time: {concatenationTime} ms" />
    
    <mx:Button label="My StringBuffer"  enabled="{dataArray!=null}" click="bufferTest()"  />
    <mx:Label text="Buffer time: {bufferTime} ms" />  
    
</mx:Application>