Sunday, October 18, 2009

Reducing CPU usage in Adobe AIR


Reducing CPU usage in Adobe AIR

Let's be honest. AIR gets a bad rap for being a bloated runtime, using up a lot of precious memory and CPU. Although a lot of AIR applications seem to fall into this trap, it doesn't have to be this way. There are a number of techniques you can use to develop a lightweight application that rivals native programs in terms of performance.
One simple and easy way to drastically reduce CPU usage is through framerate throttling. In this article, I will explain what framerate throttling is and how best to implement it in your application.
Note: To make the most of this article, you should have general knowledge of ActionScript and AIR application development.

WHAT IS FRAMERATE THROTTLING?

Framerate throttling is the technique of controlling an application's framerate to increase performance when in use and reduce resource usage when idle. As of ActionScript 3, developers have an extremely useful property in their possession—Stage.frameRate. This gem lets you change the framerate on the fly. In previous versions of ActionScript, we were stuck with what we set it to in the IDE. Thankfully, times have changed and there's no longer an excuse for processor-heavy applications lingering in the background.

HOW DO YOU IMPLEMENT FRAMERATE THROTTLING?

Since framerate throttling is essentially a matter of setting the Stage.frameRate property to a lower or higher value, it's up to the developer to decide how involved or advanced it will be. It also depends on the application itself—some allow for more integration than others.
Note: The performance results in the following examples are done on a Macbook Pro 2.8 GHz Intel Core 2 Duo. Since CPU usage is in terms of percent, results will vary from computer to computer.




Novice

The rawest form of throttling is by using the NativeApplication Event.ACTIVATE andEvent.DEACTIVATE events—increase the framerate when active, decrease it when inactive. With a single blank window, this results in 1.8% CPU usage when active and .4% when inactive. You can actually set the framerate to .01 on deactivate for .2% usage, but in testing I discovered the window chrome never loses focus.
package {
   import flash.desktop.NativeApplication;
   import flash.display.Sprite;
   import flash.events.Event;
 
   public class Application extends Sprite {
      public function Application () {
         __init ();
      }
      
      private function __init ():void {
         NativeApplication.nativeApplication.addEventListener
           (Event.ACTIVATE, __activate__);
         NativeApplication.nativeApplication.addEventListener
           (Event.DEACTIVATE, __deactivate__);
      }
      
      private function __activate__
         ($event:Event):void {
         stage.frameRate = 50;
      }
      private function __deactivate__ ($event:Event):void {
         stage.frameRate = 1;
      }
   }
}

Intermediate

Certain applications allow more advanced framerate throttling—for example, an application that still needs a level of interaction, even when in the background. Let's say your application has scrollable content to reference and since AIR allows mouse wheel scrolling while in a different application, you need a higher framerate at that time.
In this example, if the application is in the background, but the mouse wheel is scrolling, theMouseEvent.MOUSE_WHEEL handler increases the framerate and sets up anEvent.ENTER_FRAME event that will reduce the framerate half a second after scrolling. In cases like these, it's best to have a buffer in place, so you won't change the framerate with every scroll, but also because there's no event for when the mouse wheel is idle.
package {
   import flash.desktop.NativeApplication;
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.utils.getTimer;
 
   public class Application extends Sprite {
      public static const ACTIVE:int = 50;
      public static const INACTIVE:int = 1;
 
      public var active:Boolean;
      public var scrolling:Boolean;
      public var buffer:int;
      
      public function Application () {
         __init ();
      }
      
      private function __init ():void {
        NativeApplication.nativeApplication.addEventListener
        (Event.ACTIVATE, __activate__);
        NativeApplication.nativeApplication.addEventListener
        (Event.DEACTIVATE, __deactivate__);
        stage.addEventListener 
        (MouseEvent.MOUSE_WHEEL, __mouseWheel__);
      }
      
      private function __activate__ ($event:Event):void {
         active = true;
         stage.frameRate = ACTIVE;
      }
      private function __deactivate__ ($event:Event):void {
         active = false;
         stage.frameRate = INACTIVE;
      }
      private function __mouseWheel__ ($event:MouseEvent):void {
         if (!active) {
           if (!scrolling) {
              stage.addEventListener 
                (Event.ENTER_FRAME, __enterframe__);
           }
           stage.frameRate = ACTIVE;
           scrolling = true;
           buffer = getTimer () + 500;
         }
      }
      private function __enterframe__
         ($event:Event):void {
         if (buffer < getTimer ()) {
           stage.frameRate = INACTIVE;
           scrolling = false;
           stage.removeEventListener
            (Event.ENTER_FRAME, __enterframe__);
         }
      }
   }
}

Expert

If performance optimization is what you live for, you can impress your friends with some intricate framerate throttling. (Note: This won't impress girlfriends.)
In my applications, I like to have transitions from one state to the next for both a smoother environment and a better feel. Because of this, I like to use a high framerate (50). Unfortunately, the higher the framerate, the higher the CPU usage. Therefore, I set the framerate to 50 only when a tween is active. When one isn't, I reduce the framerate to 24. On top of that, there are instances when a loader is animating while the application is in the background. A loader doesn't need 50 fps, so I'll set the framerate to 5 when the application is visible in the background and 1 when not visible.
Note: For this example, I'm using an animate() method to call at the beginning of each tween. Ideally, you would want to build the framerate throttler into your tweening engine, so you wouldn't need to call animate() manually.
package {
   import flash.desktop.NativeApplication;
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.utils.getTimer;
 
   public class Application extends Sprite {
      public static const ANIMATING:int = 50;
      public static const ACTIVE:int = 24;
      public static const INACTIVE_VISIBLE:int = 5;
      public static const INACTIVE_INVISIBLE:int = 1;
      
      public var active:Boolean;
      public var animating:Boolean;
      public var buffer:int;
      
      public function Application () {
         __init ();
      }
      
      private function __init ():void {
        NativeApplication.nativeApplication.addEventListener
        (Event.ACTIVATE, __activate__);
        NativeApplication.nativeApplication.addEventListener 
        (Event.DEACTIVATE, __deactivate__);
      }
      
      public function activate ():void {
         if (!animating) {
           stage.frameRate = ACTIVE;
         }
      }
      public function deactivate ():void {
         if (!animating) {
           stage.frameRate = (stage.nativeWindow.visible) ? 
              INACTIVE_VISIBLE : INACTIVE_INVISIBLE;
         }
      }
      public function animate ($duration:int = 1000):void {
         stage.frameRate = 50;
         buffer = getTimer () + $duration;
         animating = true;
         
         if (!animating) {
           stage.addEventListener (Event.ENTER_FRAME, __checkBuffer__);
         }
      }
      
      private function __activate__ ($event:Event):void {
         active = true;
         activate ();
      }
      private function __deactivate__ ($event:Event):void {
         active = false;
         deactivate ();
      }
      private function __checkBuffer__ ($event:Event):void {
         if (buffer < getTimer ()) {
           stage.removeEventListener
           (Event.ENTER_FRAME, __checkBuffer__);
           animating = false;
           if (active) {
             activate ();
           } else {
             deactivate ();
           }
         }
      }
   }
}
Framerate throttling is a small chapter in the optimization of your AIR application's performance. It's a basic way to get your foot in the door and in the mindset of keeping resource usage low. This mentality and practice can easily lead to more responsive applications that enhance the user's experience while leaving a light footprint. Let's face it—no one likes bloatware.










0 comments :

Post a Comment

Also see this links

I am getting money from this links after signing in for free. Don't wast time just join and earn without any hard word.....
Hello Friends,
     I want to tell you about great site I found. They pay me to read e-mail, visit web sites and much more.
JOIN A CERTIFIED LEGITIMATE ONLINE BIZ!!!!
Would you like to Earn money without spending even a single penny?
Now! It is possible..
No Registration Fee
No Credit Cards required
No Hassle
No Risk
Not Nesseary to Join others
It's free to join and easy to sign up! CLICK THIS LINK TO VISIT:
Earn money without Investment
Earn money for getting ads to u r cell phone numbers what u want to get.

1) http://earnbyads.co.in/PI1.aspx?RC=5ba4f5ae
2) http://www.mginger.com/index.jsp?inviteId=883819
3) http://www.youmint.com/network-ramu4all_31
4) http://www.sms2earn.com?refid=5713
5) www.sms2india.co.in/?user=ramu4all_31
6) http://www.admad.mobi/112411-ThulasiramS.admad
7) http://www.mgarlic.com/?invite=222242142
The more time you spend surfing, the more chances of earning money.
1) http://www.earnptr.com/pages/index.php?refid=ramu4all31
3. Each time you refer a new user, you earn 10% of their image views forever!
http://www.capitalmails.net/pages/index.php?refid=ramu4all31

capitalmails.net
http://www.homepagepublicity.com/?referer=166996
http://www.jaxtr.com/ramu4all_31
Earn money without Investments:
Login for free and Earn money without any investment by clicking the below links...
I really getting money from this websites
I just joined Shelfari to connect with other book lovers. Come see the books I love and see if we have any in common. Then pick my next book so I can keep on reading. See by clicking the below link...
http://www.shelfari.com/invite/RJNuEo$a2-E6REr8y2iIPQ
http://ramu4all31.sulit.com.ph
http://www.resource-a-day.net/member/index.cgi?ramu4all_31
Earn money for reading E-mails:
The easiest way to make more money is to let others make them for you. Cashfiesta pays you for your referrals up to the 8th level. See for yourself how much you can earn, click on the Fiesta Calculator.
http://www.cashfiesta.com/php/join.php?ref=ramu4all_31
http://rolex-mails.com/pages/index.php?refid=ramu4all31
rolex-mails.com
Get paid to take surveys! Get $6 after signing up! Get $1.25 per referred friend. Low Payout! Create your account absolutely free!

http://www.AWSurveys.com/HomeMain.cfm?RefID=ramu4all31

http://www.earnptr.com/pages/index.php?refid=ramu4all31
http://www.advercash.net/signup.php?ref=ramu4all_31
http://bux.to/?r=ramu4all_31
Payout : US$ 10.00
Per Click : US$ 0.01
Per Sign up : US$ 0.30
Referrals : 50%
http://www.masterbux.com/?r=ramu4all_31
http://www.bux3.com/?r=ramu4all31
http://www.just-click.us/?r=ramu4all_31
http://clix4coins.com/index1.php?ref=ramu4all_31

http://www.gjobsdataservice.com/affiliate/idevaffiliate.php?id=812