2011年5月26日 星期四

new Object VS { } 與 new Array( ) VS [ ]


測試程式碼:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" 
      minWidth="955" minHeight="600"
      applicationComplete="completeHandler()"
      >
 <fx:Script>
  <![CDATA[
    
   private var count:int;
   private var beforeTime:int;
   private var afterTime:int;
   private var object:Object;
   private var array:Array;
   private const REPS:int = 1000000;
   
   private var enableBtn:Boolean;
   protected function completeHandler():void
   {    
    enableBtn = true;    
   }
   
   protected function get executionTime():String
   {
    return (afterTime-beforeTime).toString();
   }

   
   protected function button1_mouseDownHandler(event:MouseEvent):void
   { 
    if(!enableBtn)return;
    enableBtn = false; 
    beforeTime = getTimer();
    for (count = 0; count < REPS; count++)
    {
     object = {};
    }
    afterTime = getTimer();
    log0.text = executionTime;
    enableBtn = true; 
   }
   
   protected function button2_mouseDownHandler(event:MouseEvent):void
   { 
    if(!enableBtn)return;
    enableBtn = false; 
    beforeTime = getTimer();
    for (count = 0; count < REPS; count++)
    {
     object = new Object();
    }
    afterTime = getTimer();
    log1.text = executionTime;
    enableBtn = true; 
   }
   
   protected function button3_mouseDownHandler(event:MouseEvent):void
   { 
    if(!enableBtn)return;
    enableBtn = false; 
    beforeTime = getTimer();
    for (count = 0; count < REPS; count++)
    {
     array = [1,2,3];
    }
    afterTime = getTimer();
    log2.text = executionTime;
    enableBtn = true; 
   }
   
   protected function button4_mouseDownHandler(event:MouseEvent):void
   { 
    if(!enableBtn)return;
    enableBtn = false; 
    beforeTime = getTimer();
    for (count = 0; count < REPS; count++)
    {
     array = new Array(1,2,3);
    }
    afterTime = getTimer();
    log3.text = executionTime;
    enableBtn = true; 
   }
   
  ]]>
 </fx:Script> 
 <s:HGroup>
  <s:Panel width="200" height="150" title="\{\}"> 
   <s:layout>
    <s:VerticalLayout/>
   </s:layout>
   <s:TextArea id="log0" width="100%" height="100%"/> 
   <s:Button label="start" mouseDown="button1_mouseDownHandler(event)"/>
  </s:Panel>
  <s:Panel width="200" height="150" title="new Object">   
   <s:layout>
    <s:VerticalLayout/>
   </s:layout>
   <s:TextArea id="log1" width="100%" height="100%"/>
   <s:Button label="start" mouseDown="button2_mouseDownHandler(event)"/&gt;
  </s:Panel>
  <s:Panel width="200" height="150" title="[1,2,3]">   
   <s:layout>
    <s:VerticalLayout/>
   </s:layout>
   <s:TextArea id="log2" width="100%" height="100%"/>
   <s:Button label="start" mouseDown="button3_mouseDownHandler(event)"/>
  </s:Panel>
  <s:Panel width="200" height="150" title="new Array(1,2,3)">   
   <s:layout>
    <s:VerticalLayout/>
   </s:layout>
   <s:TextArea id="log3" width="100%" height="100%"/>
   <s:Button label="start" mouseDown="button4_mouseDownHandler(event)"/>
  </s:Panel>
 </s:HGroup> 
</s:Application>


//結果






new Object( )   < {}

new Array(1,2,3)   >  [1,2,3]

//性能概要分析

沒有留言:

張貼留言