SimpleLoop – as

simpleloop-as.swf

The programme:

SimpleLoop.as
package {
   import flash.display.Sprite;
   import flash.utils.Timer;
   import flash.utils.getTimer;
   import flash.utils.getTimer;
   import flash.text.TextField;


  [SWF(width="200", height="100", backgroundColor="#eeeeee")]
   public class SimpleLoop extends Sprite
   {
      function SimpleLoop()
      {
         var timer:int = getTimer();
         var sum:int = Run1();
         timer = getTimer()-timer;

         var display:TextField = new TextField();
         display.x=10;
         display.y=10;
         display.width=190;
         display.text = "AS3 for :" + timer + " ms = " + sum;
         addChild(display);

         timer = getTimer();
         var sum2:int = Run2();
         timer = getTimer()-timer;

         var display2:TextField = new TextField();
         display2.x=10;
         display2.y=50;
         display2.width=190;
         display2.text = "AS3 while :" + timer + " ms = " + sum2;
         addChild(display2);

      }

      internal function Run1() : int
      {
         var sum:int = 0;
         for(var j:int = 0; j<1000;j++)
         {
            sum = 0;
            for(var i:int = 0; i<10000; i++)
               sum += i;
         }
         return sum;
      }

      internal function Run2() : int
      {
         var sum:int = 0;
         var j:int = 0;
         while(j < 1000)
         {
            sum = 0;
            var i:int = 0;
            while(i<10000)
            {
               sum += i;
               i++;
            }
            j++;
         }
         return sum;
      }

    }

}

One Reply to “SimpleLoop – as”

Leave a Reply

Your email address will not be published. Required fields are marked *