<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jozef Chúťka&#039;s blog &#187; optimization</title>
	<atom:link href="http://blog.yoz.sk/tag/optimization/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.yoz.sk</link>
	<description>My life, my work</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:40:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Myth Buster &#8211; Benchmarking Loops</title>
		<link>http://blog.yoz.sk/2010/05/myth-buster-benchmarking-loops/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=myth-buster-benchmarking-loops</link>
		<comments>http://blog.yoz.sk/2010/05/myth-buster-benchmarking-loops/#comments</comments>
		<pubDate>Thu, 13 May 2010 14:23:12 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[linked list]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[Vector]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=1627</guid>
		<description><![CDATA[Lets have a look at some of the most common and fastest loops used in ActionScript 3. Is int really faster than uint and Number? Is while loop faster than for? Is pre-increment faster than post-increment? To test all of these I have created benchmark test application on wonderfl. App compares loops with 10.000.000 iterations [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.yoz.sk/wp-content/uploads/benchmark.jpg" alt="" title="benchmark" width="200" height="100" class="alignleft size-full wp-image-1629" /></p>
<p>Lets have a look at some of the most common and fastest loops used in ActionScript 3. Is int really faster than uint and Number? Is while loop faster than for? Is pre-increment faster than post-increment? To test all of these I have created benchmark test application on wonderfl. App compares loops with 10.000.000 iterations and logs testing times. To compare different variable types, loop methods, increment / decrement modes, there are buttons grouping each test. Please be patient with your first click, the model Array is being generated (Link button also generates linked list). The first test compares Arrays, second one Vectors.</p>
<p><span id="more-1627"></span></p>
<p>Loop Benchmark Test 1 uses Array:</p>
<div style="text-align:center;width:465px;"><iframe title="Loop Benchmark Test 1 - wonderfl build flash online" scrolling="no" src="http://wonderfl.net/blogparts/1Iva" width="465" height="490" style="border:1px black solid;"></iframe><a href="http://wonderfl.net/c/1Iva" title="Loop Benchmark Test 1 - wonderfl build flash online">Loop Benchmark Test 1 &#8211; wonderfl build flash online</a></div>
<p>Loop Benchmark Test 2 uses Vector.<uint>:</p>
<div style="text-align:center;width:465px;"><iframe title="Loop Benchmark Test 2 - wonderfl build flash online" scrolling="no" src="http://wonderfl.net/blogparts/6YQB" width="465" height="490" style="border:1px black solid;"></iframe><a href="http://wonderfl.net/c/6YQB" title="Loop Benchmark Test 2 - wonderfl build flash online">Loop Benchmark Test 2 &#8211; wonderfl build flash online</a></div>
<p>Loop Benchmar Test 4: Linked List vs. Final Linked List<uint>:</p>
<div style="text-align:center;width:465px;"><iframe title=" Loop Benchmar Test 4: Linked List vs. Final Linked List - wonderfl build flash online" scrolling="no" src="http://wonderfl.net/blogparts/lFkm" width="465" height="490" style="border:1px black solid;"></iframe><a href="http://wonderfl.net/c/lFkm" title=" Loop Benchmar Test 4: Linked List vs. Final Linked List - wonderfl build flash online"> Loop Benchmar Test 4: Linked List vs. Final Linked List &#8211; wonderfl build flash online</a></div>
<p>To sum it up fastest seems to be these methods:</p>
<pre class="brush: plain; title: ; notranslate">Arrays
testForUintPreDecrement: 78 ms. *GLOBAL FASTEST*
testForUintPostDecrement: 78 ms. *GLOBAL FASTEST*
testForIntPostDecrement: 78 ms. *GLOBAL FASTEST*
testWhileUintPreDecrement: 78 ms. *GLOBAL FASTEST*
testWhileUintPostDecrement: 78 ms. *GLOBAL FASTEST*

Vectors
testForUintPreDecrement: 62 ms. *GLOBAL FASTEST*
testForIntPreDecrement: 62 ms. *GLOBAL FASTEST*
testForIntPostDecrement: 62 ms. *GLOBAL FASTEST*
testWhileIntPreIncrement: 62 ms. *GLOBAL FASTEST*
testWhileIntPostIncrement: 62 ms. *GLOBAL FASTEST*
testLinked: 62 ms. *GLOBAL FASTEST*</pre>
<p>Those are my favorits:</p>
<pre class="brush: as3; title: ; notranslate">public function testForIntPreDecrement():void
{
	for(var i:int = R - 1; i &gt; -1; --i)
		modelArray[i];
}

public function testWhileUintPostDecrement():void
{
	var i:uint = modelArray.length;
	while(i--)
		modelArray[i];
}

public function testLinked():void
{
	var link:Linked = firstLink;
	while(link)
		link = link.next;
}</pre>
<p>&#8230; that says it all:</p>
<ul>
<li>&#8220;for&#8221; and &#8220;while&#8221; loops can be equally fast, while &#8220;for each&#8221; loop is much slower</li>
<li>&#8220;uint&#8221; and &#8220;int&#8221; is equally fast, and is much faster than &#8220;Number&#8221;</li>
<li>&#8220;pre&#8221; vs. &#8220;post&#8221; is more less the same</li>
<li>decrement mostly wins over increment, but in some cases is equally fast</li>
<li>Linked-lists iteration performance equals to Vector and is better than Arryas</li>
<li>when casting required, linked lists rules them all</li>
<li>using ByteArray as list is not as fast as Array nor Vector</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2010/05/myth-buster-benchmarking-loops/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Common Tricks in ActionScript Code Shortening</title>
		<link>http://blog.yoz.sk/2010/03/common-tricks-in-actionscript-code-shortening/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=common-tricks-in-actionscript-code-shortening</link>
		<comments>http://blog.yoz.sk/2010/03/common-tricks-in-actionscript-code-shortening/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 12:04:07 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[assignments]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[instations]]></category>
		<category><![CDATA[operators]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[statements]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=1274</guid>
		<description><![CDATA[Code shortening can make your code more readable as well as more unreadable . While it is recommend to use coding conventions and standards, in some cases you can spare a lot of lines using some shortening. In fact there must not be a conflict between using convetion vs. using shortening, and soon after you [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.yoz.sk/wp-content/uploads/shortening.jpg" alt="" title="shortening" width="200" height="100" class="alignleft size-full wp-image-1292" /></p>
<p>Code shortening can make your code more readable as well as more unreadable <img src='http://blog.yoz.sk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . While it is recommend to use <a href="http://opensource.adobe.com/wiki/display/flexsdk/Coding+Conventions">coding conventions and standards</a>, in some cases you can spare a lot of lines using some shortening. In fact there must not be a conflict between using convetion vs. using shortening, and soon after you get used to some &#8220;shortcuts&#8221;, your code may be reduced in reasonable amount of lines. Some reading about <a href="http://blog.yoz.sk/2009/10/code-optimization/">code optimization</a> can be found here, this article extends optimizations.</p>
<p><span id="more-1274"></span></p>
<h3><a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/statements.html#with">with() statement</a></h3>
<p>Establishes a default object to be used for the execution of a statement or statements, potentially reducing the amount of code that needs to be written. You can use multiple with() statemens:</p>
<pre class="brush: as3; title: ; notranslate">with(new Date)
with(Math)
    trace(hours, sin(0));</pre>
<h3>List style dynamic switch</h3>
<p>Sometimes when you need to &#8220;switch&#8221; simple input value into output value, all you need is to choose item from list.</p>
<pre class="brush: as3; title: ; notranslate">var m:uint = 3;
trace(['','I','II','III','IV'][m]);
trace({a:'AA',b:'BB'}['b']);</pre>
<h3>New instantion / addChild combo</h3>
<p>While DisplayObjectContainer.addChild(child) returns inserted child as DisplayObject, you can cast it.</p>
<pre class="brush: as3; title: ; notranslate">var s:Sprite = Sprite(addChild(new Sprite()))</pre>
<h3>Anonymous functions</h3>
<p>ActionScript lets you put function definition directly where you normally put reference.</p>
<pre class="brush: as3; title: ; notranslate">addEventListener(Event.ENTER_FRAME, function(event:Event):void
{
    trace(&quot;anonymous enter frame&quot;)
});</pre>
<p>In order to get a reference to the currently executing function use <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/arguments.html#callee">arguments.callee</a>. If you need to removeEventListener, use reference not direct definition.</p>
<h3>Omitting curly braces</h3>
<p>It is possible to omit curly braces with some block statements (for, for each, if, while) on single statement</p>
<pre class="brush: as3; title: ; notranslate">for(var i:uint=0;i&lt;100;i++)
    trace(i)</pre>
<h3>Joining multiple statements into single-like</h3>
<p>In order to create single statement from multiple statement (to omit curly braces) join statements with &#8220;,&#8221;.</p>
<pre class="brush: as3; title: ; notranslate">var i:uint;
while(i&lt;10)
    i++,trace(i);</pre>
<h3><a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/operators.html#conditional">Ternar / conditional operator</a></h3>
<p>Pretty common, but worth mentioning</p>
<pre class="brush: as3; title: ; notranslate">for(var i:uint = 0; i &lt; 3; i++)
    trace(i == 1 ? &quot;BOMB&quot; : i);</pre>
<h3><a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/operators.html">Operators</a> (pre-increment, post-increment&#8230;)</h3>
<p>Also pretty common</p>
<pre class="brush: as3; title: ; notranslate">var i:uint = 0;
trace(i++, ++i, i--, --i);
i += 1, i *= 2, i /= 2, i -= 1, i %= 2;</pre>
<h3><a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/operators.html#modulo">Modulo operator (%)</a> and alternation</h3>
<p>The Modulus operator(%) computes the remainder of division between 2 integers. Can be use for alternation (color etc.)</p>
<pre class="brush: as3; title: ; notranslate">for(var i:uint = 0; i &lt; 10; i++)
    trace(i%3 ? i : &quot;BOMB&quot;);</pre>
<h3>Action assignment</h3>
<p>Result of assignment is value itself.</p>
<pre class="brush: as3; title: ; notranslate">var i:uint, j:uint = 0;
trace(i = j = 2);</pre>
<h3><a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/statements.html#for">for statement</a></h3>
<p>for statement definition says:</p>
<pre class="brush: plain; title: ; notranslate">for ([init]; [condition]; [next])</pre>
<p>&#8230; it means you can use it very creative way</p>
<pre class="brush: as3; title: ; notranslate">var i:uint = 0;
with(graphics)
for(clear(),lineStyle(1,0);i&lt;10;x=y=100,i++)
    lineTo(x*i, y*i*i);</pre>
<h3><a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/operators.html#logical_OR">logical OR operator (||)</a> and first valid</h3>
<p>Result of statement using logical OR is the first valid element. It can substitute ternar operator in some cases</p>
<pre class="brush: as3; title: ; notranslate">trace(null || 0 || &quot;&quot; || &quot;BOMB&quot; || &quot;never&quot;);
trace(defined ? defined : &quot;default&quot;) // equals to trace(defined || &quot;default&quot;)</pre>
<p>You can use || to shorten Boolean resulting ternar operator:</p>
<pre class="brush: as3; title: ; notranslate">test ? true : false;
test || false;
test || 0;</pre>
<h3>Class Instantiating without &#8220;new&#8221;</h3>
<p>Most common used is string delimiter (&#8220;), but there are also ones for XML, RegExp and Array</p>
<pre class="brush: as3; title: ; notranslate">var x:XML = &lt;data&gt;&lt;/data&gt;;
var r:RegExp = /.*/i;</pre>
<p>Shortest declaring for Array and <a href="http://jacksondunstan.com/articles/702">Vector</a>:</p>
<pre class="brush: as3; title: ; notranslate">var a:Array = [1,2,3];
var v:Vector.&lt;int&gt; = new &lt;int&gt;[1,2,3]</pre>
<h3>Inline conditions</h3>
<p>Use short circuit conditioning instead of if clause.</p>
<pre class="brush: as3; title: ; notranslate">if(condition)
    doSomething();
// is the same as
condition &amp;&amp; doSomething();</pre>
<h3>Boolean-izator (!!)</h3>
<p>I guess the shortest way to get Boolean results.</p>
<pre class="brush: as3; title: ; notranslate">!!test</pre>
<h3>Other</h3>
<ul>
<li><a href="http://livedocs.adobe.com/flex/3/html/help.html?content=13_Working_with_XML_03.html#119488">E4X approach to XML processing</a>, <a href="http://www.sephiroth.it/tutorials/flashPHP/E4X/">examples</a></li>
<li><a href="http://blog.yoz.sk/2009/10/code-optimization/">Code optimization</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2010/03/common-tricks-in-actionscript-code-shortening/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Frame rate optimization</title>
		<link>http://blog.yoz.sk/2009/11/frame-rate-optimization/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=frame-rate-optimization</link>
		<comments>http://blog.yoz.sk/2009/11/frame-rate-optimization/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 15:05:49 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[active]]></category>
		<category><![CDATA[Application]]></category>
		<category><![CDATA[BitmapData]]></category>
		<category><![CDATA[frameRate]]></category>
		<category><![CDATA[FrameRateOptimizator]]></category>
		<category><![CDATA[inactive]]></category>
		<category><![CDATA[MouseEvent]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[Stage]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=604</guid>
		<description><![CDATA[There has been a lot of articles written about how to optimize your flash / flex application frameRate. In most cases, you only need to have full frameRate when some animation is playing, or tweening etc., in other words when some visual changes are going on. FrameRateOptimizator class does exactly what you need. In default, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.yoz.sk/wp-content/uploads/graph-going-up.jpg"><img src="http://blog.yoz.sk/wp-content/uploads/graph-going-up-200x199.jpg" alt="graph-going-up" title="graph-going-up" width="200" height="199" class="alignleft size-medium wp-image-610" /></a></p>
<p>There has been a lot of <a href="http://theflashblog.com/?p=1138">articles written about how to optimize your flash / flex application frameRate</a>. In most cases, you only need to have full frameRate when some animation is playing, or tweening etc., in other words when some visual changes are going on. FrameRateOptimizator class does exactly what you need. In default, it uses full frameRate when visual changes and when it stops, optimizator automatically switches to lower frameRate. Optimizator uses bitmapData to compare last state with actual state to catch any visual changes. By default snapshot is executed two times per second in low frameRate (inactive state), or one time per second in full frameRate state (active state), taking approximately not more than 10ms per execution (fast enough).</p>
<ul style="clear:both;">
<li><a href="http://classes.yoz.sk/sk/yoz/optimization/FrameRateOptimizator.as">sk.yoz.optimization.FrameRateOptimizator</a></li>
<li>Application</li>
</ul>
<p><span id="more-604"></span></p>
<p>FrameRateOptimizator.as</p>
<pre class="brush: as3; title: ; notranslate">package sk.yoz.optimization
{
    import flash.display.BitmapData;
    import flash.display.DisplayObject;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.MouseEvent;
    import flash.events.TimerEvent;
    import flash.geom.Matrix;
    import flash.utils.Timer;

    public class FrameRateOptimizator extends EventDispatcher
    {
        public var frameRateActive:uint = 24;
        public var frameRateInactive:uint = 2;

        protected var snapshot:BitmapData;

        protected var fpsp:uint;
        protected var _fps:uint;
        protected var fpsTimer:Timer;

        protected var root:DisplayObject;
        protected var _forceActive:Boolean = false;
        protected var _forceInactive:Boolean = false;
        protected var _mouseMoveActivation:Boolean = false;
        protected var _samplingScale:Number = 1;
        protected var samplingMatrix:Matrix = new Matrix();

        protected var samplingPeriod:uint = 1000;
        protected var activityTimer:Timer;

        public var debug:Boolean = false;

        public function FrameRateOptimizator(root:DisplayObject,
            frameRateActive:uint = 24, frameRateInactive:uint = 2,
            samplingPeriod:uint = 1000, mouseMoveActivation:Boolean = false,
            samplingScale:Number = 1):void
        {
            super();

            this.root = root;
            this.frameRateActive = frameRateActive;
            this.frameRateInactive = frameRateInactive;
            this.samplingPeriod = samplingPeriod;
            this.mouseMoveActivation = mouseMoveActivation;
            this.samplingScale = samplingScale;

            root.addEventListener(Event.ENTER_FRAME, rootEnterFrame);

            fpsTimer = new Timer(1000);
            fpsTimer.addEventListener(TimerEvent.TIMER, fpsTimerHandler);
            fpsTimer.start();

            var type:String = TimerEvent.TIMER_COMPLETE;
            activityTimer = new Timer(samplingPeriod, 1);
            activityTimer.addEventListener(type, activityTimerComplete);

            isActive = true;
        }

        [Bindable(event=&quot;fpsChanged&quot;)]
        public function get fps():uint
        {
            return _fps;
        }

        protected function set fps(value:uint):void
        {
            if(_fps == value)
                return;
            _fps = value;
            dispatchEvent(new Event(&quot;fpsChanged&quot;));
        }

        public function set forceActive(value:Boolean):void
        {
            _forceActive = value;
            if(forceActive)
                isActive = true;
        }

        public function get forceActive():Boolean
        {
            return _forceActive;
        }

        public function set forceInactive(value:Boolean):void
        {
            _forceInactive = value;
            if(forceInactive)
                isActive = false;
        }

        public function get forceInactive():Boolean
        {
            return _forceInactive;
        }

        public function set mouseMoveActivation(value:Boolean):void
        {
            if(value == _mouseMoveActivation)
                return;
            _mouseMoveActivation = value;
            if(value)
                root.addEventListener(MouseEvent.MOUSE_MOVE, rootMouseMove);
            else
                root.removeEventListener(MouseEvent.MOUSE_MOVE, rootMouseMove);
        }

        public function get mouseMoveActivation():Boolean
        {
            return _mouseMoveActivation;
        }

        public function set isActive(value:Boolean):void
        {
            if(value &amp;&amp; !forceActive &amp;&amp; !forceInactive)
            {
                activityTimer.reset();
                activityTimer.start();
            }
            frameRate = suggestFrameRate(value);
        }

        public function set samplingScale(value:Number):void
        {
            _samplingScale = value;
            samplingMatrix = new Matrix();
            samplingMatrix.scale(value, value);
        }

        public function get samplingScale():Number
        {
            return _samplingScale;
        }

        protected function suggestFrameRate(active:Boolean):uint
        {
            if(forceActive)
                return frameRateActive;
            if(forceInactive)
                return frameRateInactive;
            return active ? frameRateActive : frameRateInactive;
        }

        protected function set frameRate(value:uint):void
        {
            if(value == frameRate)
                return;
            root.stage.frameRate = value;
            if(debug)
                trace(&quot;FrameRateOptimizator -&gt; frameRate = &quot; + value);
        }

        protected function get frameRate():uint
        {
            return root.stage.frameRate;
        }

        public function get isActive():Boolean
        {
            return frameRate == frameRateActive;
        }

        protected function rootEnterFrame(event:Event):void
        {
            fpsp++;
            if(!isActive &amp;&amp; rootChanged)
                isActive = true;
        }

        protected function rootMouseMove(event:MouseEvent):void
        {
            isActive = true;
        }

        protected function get rootChanged():Boolean
        {
            var changed:Boolean = true;
            var bitmapData:BitmapData;
            try
            {
                var d0:Date = new Date();
                bitmapData = new BitmapData(
                    root.width * samplingScale, root.height * samplingScale);
                bitmapData.draw(root, samplingMatrix);
                if(snapshot)
                    changed = bitmapData.compare(snapshot) != 0;
                snapshot = bitmapData;
                trace(new Date().time - d0.time, &quot;ms&quot;)
            }
            catch(error:Error)
            {
                snapshot = null;
                if(debug)
                    trace(&quot;FrameRateOptimizator Error: &quot; + error.message);
            }
            return changed;
        }

        protected function fpsTimerHandler(event:TimerEvent):void
        {
            protected::fps = fpsp;
            fpsp = 0;
        }

        protected function activityTimerComplete(event:TimerEvent):void
        {
            isActive = rootChanged;
        }
    }
}</pre>
<p>Application</p>
<pre class="brush: xml; title: ; notranslate">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
    applicationComplete=&quot;init()&quot;&gt;
&lt;mx:Script&gt;
&lt;![CDATA[
    import sk.yoz.optimization.FrameRateOptimizator;

    [Bindable]
    private var frameRateOptimizator:FrameRateOptimizator;

    private function init():void
    {
        frameRateOptimizator = new FrameRateOptimizator(this, 30);

        // trace new frameRate on change
        frameRateOptimizator.debug = true;

        // makes active frameRate on mouse move
        frameRateOptimizator.mouseMoveActivation = true;

        // always use active frameRate
        // frameRateOptimizator.forceActive = true;

        // always use inactive frameRate
        // frameRateOptimizator.forceInactive = true;

        // use active frameRate immediately
        //frameRateOptimizator.isActive = true;
    }
]]&gt;
&lt;/mx:Script&gt;
&lt;mx:VSlider/&gt;
&lt;/mx:Application&gt;</pre>
<p>Export code in debug mode and watch how frameRate changes (in trace console) when playing with slider. Try (un)comment mouseMoveActivation and see the difference.</p>
<p>Optimizer will not work if application contains any source object and (in the case of a Sprite or MovieClip object) all of its child objects do not come from the same domain as the caller, or are not in a content that is accessible to the caller by having called the Security.allowDomain() method. <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html#draw()">Read more about BitmapData.draw() SecurityError</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2009/11/frame-rate-optimization/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Quick tip: Array vs. Vector</title>
		<link>http://blog.yoz.sk/2009/11/quick-tip-array-vs-vector/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=quick-tip-array-vs-vector</link>
		<comments>http://blog.yoz.sk/2009/11/quick-tip-array-vs-vector/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 10:00:34 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[Vector]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=583</guid>
		<description><![CDATA[Take a closer look at the ActionScript 3 memory consumption, when stored huge amount of values in Vector and Array. First list of uints: &#8230; and list of Booleans: Read more about AVM2 Memory Considerations.]]></description>
			<content:encoded><![CDATA[<p>Take a closer look at the ActionScript 3 memory consumption, when stored huge amount of values in Vector and Array. First list of uints:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
(10000000 / 32) uints in a Vector (10000000 bits) = ~7k
(10000000 / 32) uints in an Array (10000000 bits) = ~1228k
</pre>
<p>&#8230; and list of Booleans:</p>
<pre class="brush: plain; gutter: false; title: ; notranslate">
10000000 Booleans in a Vector = ~8k
10000000 Booleans in an Array = ~39070k
</pre>
<p>Read more about <a href="http://blog.generalrelativity.org/actionscript-30/avm2-memory-considerations-and-bit-vectors/">AVM2 Memory Considerations</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2009/11/quick-tip-array-vs-vector/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apparat (TAAS) framework to optimize swf</title>
		<link>http://blog.yoz.sk/2009/10/apparat-taas-framework-to-optimize-swf/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=apparat-taas-framework-to-optimize-swf</link>
		<comments>http://blog.yoz.sk/2009/10/apparat-taas-framework-to-optimize-swf/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 09:33:38 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Technologies]]></category>
		<category><![CDATA[Apparat]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[swc]]></category>
		<category><![CDATA[swf]]></category>
		<category><![CDATA[TAAS]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=202</guid>
		<description><![CDATA[Joa Ebert is a Flash developer with a focus on ActionScript 3. He has developed several tools helping to optimize daily workflow. Most projects have been released as open-source. The most important one is Apparat / TAAS project: Apparat is an open source framework written in Java to optimize ABC, SWC and SWF files. The [...]]]></description>
			<content:encoded><![CDATA[<p>Joa Ebert is a Flash developer with a focus on ActionScript 3. He has developed several tools helping to optimize daily workflow. Most projects have been released as open-source. The most important one is Apparat / TAAS project:</p>
<p><a href="http://code.google.com/p/apparat/">Apparat</a> is an open source framework written in Java to optimize ABC, SWC and SWF files. The core of Apparat are its three file formats and their implementation:</p>
<ul>
<li>ABC (read and write ABC files, bytecode analysis and tools)</li>
<li>SWC (read and write SWC files)</li>
<li>SWF (read and write SWF files, tag implementations)</li>
</ul>
<p><strong>TAAS</strong> is a stackless representation of the ActionScriptBytecode. Read <a href="http://blog.joa-ebert.com/2009/09/01/first-results-of-taas/">first results of TAAS</a>, and see application <a href="http://www.joa-ebert.com/swf/index.php?swf=taas/b00">before</a> and <a href="http://www.joa-ebert.com/swf/index.php?swf=taas/a00">after</a> optimization.</p>
<p><span id="more-202"></span></p>
<blockquote><p>The TAAS compiler is different from the ActionScript compiler since its input is not ActionScript source code but already compiled SWF or SWC files. Just like the haXe compiler can output AS3 instead of a SWF the TAAS compiler can do the same. Now if you add one and one together you see that the TAAS compiler can be used as a very strong decompiler. My own tests have shown that it will work flawlessly where other commercial decompilers output rubbish. Since the compiler behaves like the Flash Player it will “execute” the bytecode in order to parse it which means it has a very highlevel understanding of the structure inside the SWF. (<a href="http://blog.joa-ebert.com/2009/09/29/taas-as-a-decompiler/">Joa Ebert</a>)</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2009/10/apparat-taas-framework-to-optimize-swf/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Code optimization</title>
		<link>http://blog.yoz.sk/2009/10/code-optimization/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=code-optimization</link>
		<comments>http://blog.yoz.sk/2009/10/code-optimization/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 12:25:36 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[break]]></category>
		<category><![CDATA[continue]]></category>
		<category><![CDATA[nesting]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=339</guid>
		<description><![CDATA[First, my golden rule: method with more than 12 rows is too long. Methods should be short, simple, clean and reusable. In most cases your methods fits into 5-12 rows. If not, you should consider rewriting your methods. Look at the next code. There is too much nested tests. Soon you get lost with more [...]]]></description>
			<content:encoded><![CDATA[<p>First, my golden rule: <strong>method with more than 12 rows is too long</strong>. Methods should be short, simple, clean and reusable. In most cases your methods fits into 5-12 rows. If not, you should consider rewriting your methods.</p>
<p>Look at the next code. There is too much nested tests. Soon you get lost with more and more nested levels:</p>
<pre class="brush: as3; title: ; notranslate">function gimmeResult(a, b, c)
{
    var result;
    if(a)
    {
        if(b || c)
        {
            if(b &amp;&amp; c &lt; 0)
            {
                result = 1;
            }
            else
            {
                result = 2;
                // where am I?
            }
        }
        else
        {
            result = 3;
        }
    }
    else
    {
        result = 4;
    }
    return result;
}</pre>
<p>Try to linearize code, it is much more cleaner for reading when no multiple nesting used:</p>
<pre class="brush: as3; title: ; notranslate">function gimmeResult(a, b, c)
{
    if(!a)
        return 4;
    if(!b &amp;&amp; !c)
        return 3;
    if(b &amp;&amp; c &lt; 0)
        return 1;
    return 2;
}</pre>
<p><span id="more-339"></span></p>
<p>Sometimes your function does more things than it should. In this case, it chooses item and operate over it. But is it really good to use it this way?</p>
<pre class="brush: as3; title: ; notranslate">function doSomething(key)
{
    for(i in list)
    {
        if(i == key)
        {
            if(item[i].value &gt; 0)
            {
                item[i].value2 = 1;
            }
            else
            {
                item[i].value2 = 2;
            }
            if(item[i].value3 == 100)
            {
                while(item[i].value3 &gt; 0)
                {
                    // where the hell I am now
                    item[i].value3--;
                }
            }
        }
    }
}</pre>
<p>Split your code into separated functions. One selects your item, other operates over item. Result code is more cleaner and reusable.</p>
<pre class="brush: as3; title: ; notranslate">function doSomething(key)
{
    item = getItem(key);
    if(!item)
        return;
    item.value2 = item.value &gt; 0 ? 1 : 2;
    if(item.value3 != 100)
        return;
    while(item.value3 &gt; 0)
        item.value3--;
}

function getItem(key)
{
    for(i in list)
        if(i == key)
            return list[i];
    return null;
}</pre>
<p>Using <strong>continue</strong> in while cycle causes the current iteration to skip and go to the next one. Sometimes you do not need more processing from current iteration:</p>
<pre class="brush: as3; title: ; notranslate">while(someIterator)
{
    // some processes here
    if(someIterator != &quot;someValue&quot;)
    {
        // some processes here
        // and here
        // and here
    }
}
</pre>
<p>Using <strong>continue</strong> removes nesting, code is cleaner:</p>
<pre class="brush: as3; title: ; notranslate">while(someIterator)
{
    // some processes here
    if(someIterator == &quot;someValue&quot;)
        continue;
    // some processes here
    // and here
    // and here
}
</pre>
<p>When you do not need more iterations&#8230;</p>
<pre class="brush: as3; title: ; notranslate">i = 100;
j = 0;
while(i)
{
    if(i &gt; 90)
        j++;
    i--;
}
// 100 iterations means 100 &quot;i &gt; 50&quot; tests
</pre>
<p>&#8230; you can use <strong>break</strong> to stop current while cycle and save unnecessary processing:</p>
<pre class="brush: as3; title: ; notranslate">i = 100;
j = 0;
while(i)
{
    if(i &lt;= 90)
        break;
    j++;
    i--;
}
// 10 iterations means 10 &quot;i &lt;= 50&quot; tests</pre>
<p>Where to go from here:</p>
<ul>
<li><a href="http://blog.yoz.sk/2010/05/myth-buster-benchmarking-loops/">Myth Buster – Benchmarking Loops</a></li>
<li><a href="http://www.insideria.com/2009/04/51-actionscript-30-and-flex-op.html">Round up of ActionScript 3.0 and Flex optimization techniques and practices</a></li>
<li><a href="http://help.adobe.com/en_US/as3/mobile/flashplatform_optimizing_content.pdf">Optimizing Performance for theADOBE® FLASH® PLATFORM</a></li>
<li><a href="http://jacksondunstan.com/articles/663">The Size of Empty</a></li>
<li><a href="http://jpauclair.net/2010/03/15/flash-asm/">Faster arithmetic &#8211; Flash Assembler (not ByteCode) : And you thought it couldn’t get faster</a></li>
<li><a href="http://www.gskinner.com/talks/quick/">GSkinner Talks</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2009/10/code-optimization/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

