1 Reply Latest reply on Sep 20, 2007 4:57 PM by Edward Kandrot

    AS3 performance at funciton levels

    Edward Kandrot
      Hello,

      I am new to AS3, but I heard that depending on call order, one could see a difference in performance. So, I wrote a simple test (attached) and am seeing some weird results. I am wondering if anyone else has see this, or can verify that they see this on their machines as well.

      A summary: if one function calls another function, one of them will be almost 2x faster with the same code.

      In my example, I have a simple loop that is timed. After it it is done, it calls another function that has the same loop and is also timed. Then after this completes, it calls another function, etc. Here are the results:

      Start
      Stop: 4999999950000001 1.352
      Start
      Stop: 4999999950000001 0.744
      Start
      Stop: 4999999950000001 1.422
      Start
      Stop: 4999999950000001 0.738

      The first number is just to verify that they are calculating the same value within the loop. The second number is the number of seconds it took to do the same calculation. I have tried a couple of variations on the loop, using int vs uint vs Number, simple vs complex calculations, etc - this question is a basic summary of the core issue - variance in timings on the same code.

      Any thoughts?

      -Edward

      package {
      import flash.display.Sprite;
      import flash.events.Event;
      import flash.events.MouseEvent;
      import flash.utils.getTimer;

      public class test1 extends Sprite {

      public function test1() {
      trace( "Start" );
      var time:Number = getTimer();
      var x:Number = 1;
      for( var i:Number = 0; i<100000000; i++) {
      x+=i;
      }
      time = (getTimer() - time) / 1000;
      trace( "Stop: ", x, " ", time );
      init();
      }
      private function init() {
      trace( "Start" );
      var time:Number = getTimer();
      var x:Number = 1;
      for( var i:Number = 0; i<100000000; i++) {
      x += i;
      }
      time = (getTimer() - time) / 1000;
      trace( "Stop: ", x, " ", time );
      init2();
      }
      private function init2() {
      trace( "Start" );
      var time:Number = getTimer();
      var x:Number = 1;
      for( var i:Number = 0; i<100000000; i++) {
      x += i;
      }
      time = (getTimer() - time) / 1000;
      trace( "Stop: ", x, " ", time );
      init3();
      }
      private function init3() {
      trace( "Start" );
      var time:Number = getTimer();
      var x:Number = 1;
      for( var i:Number = 0; i<100000000; i++) {
      x += i;
      }
      time = (getTimer() - time) / 1000;
      trace( "Stop: ", x, " ", time );
      }
      }
      }