<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: For Vs. While</title>
	<atom:link href="http://jacksondunstan.com/articles/486/feed" rel="self" type="application/rss+xml" />
	<link>http://jacksondunstan.com/articles/486</link>
	<description>Mastering AS3</description>
	<lastBuildDate>Tue, 07 Feb 2012 09:30:08 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: jackson</title>
		<link>http://jacksondunstan.com/articles/486/comment-page-1#comment-10325</link>
		<dc:creator>jackson</dc:creator>
		<pubDate>Tue, 09 Aug 2011 17:37:20 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=486#comment-10325</guid>
		<description>Since the performance is identical (even in the &lt;a href=&quot;/articles/997&quot; rel=&quot;nofollow&quot;&gt;update&lt;/a&gt; article), I&#039;d say it&#039;s a matter of highly-subjective readability preference. Many people find comfort in seeing the loop initialization, check, and update all in one line because &lt;em&gt;usually&lt;/em&gt; all three are required and the form acts as a convenient little checklist. If you don&#039;t need one, it&#039;s usually the initialization and then it&#039;s still easy to make a two-part checklist:

&lt;pre lang=&quot;actionscript3&quot;&gt;
for (; i &lt; len; ++i)
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Since the performance is identical (even in the <a href="/articles/997" rel="nofollow">update</a> article), I&#8217;d say it&#8217;s a matter of highly-subjective readability preference. Many people find comfort in seeing the loop initialization, check, and update all in one line because <em>usually</em> all three are required and the form acts as a convenient little checklist. If you don&#8217;t need one, it&#8217;s usually the initialization and then it&#8217;s still easy to make a two-part checklist:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> len<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Morg.</title>
		<link>http://jacksondunstan.com/articles/486/comment-page-1#comment-10318</link>
		<dc:creator>Morg.</dc:creator>
		<pubDate>Tue, 09 Aug 2011 14:46:53 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=486#comment-10318</guid>
		<description>In a way, for is syntactic sugar on top of while ... but why ?

It looks to me that while($i&lt;$max) is much more straightforward as it explicitly shows that the CPU is going to have to compare those values on each iteration (and nothing else) ... 

Even though most people seem to believe for should be used in every one of these cases, isn&#039;t it much more logical to actually use the while loop, which is clearly more explicit on what it&#039;s asking the CPU to do ?

What is your point of view on those aspects when considering performance and precision, including foreach() which hides the $i although it&#039;s using it internally anyway ?</description>
		<content:encoded><![CDATA[<p>In a way, for is syntactic sugar on top of while &#8230; but why ?</p>
<p>It looks to me that while($i&lt;$max) is much more straightforward as it explicitly shows that the CPU is going to have to compare those values on each iteration (and nothing else) &#8230; </p>
<p>Even though most people seem to believe for should be used in every one of these cases, isn&#039;t it much more logical to actually use the while loop, which is clearly more explicit on what it&#039;s asking the CPU to do ?</p>
<p>What is your point of view on those aspects when considering performance and precision, including foreach() which hides the $i although it&#039;s using it internally anyway ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jackson</title>
		<link>http://jacksondunstan.com/articles/486/comment-page-1#comment-1233</link>
		<dc:creator>jackson</dc:creator>
		<pubDate>Mon, 01 Nov 2010 06:25:58 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=486#comment-1233</guid>
		<description>The JS test is the same as the AS2 and AS3 tests in that all of them use essentially the same loop code. I like to think of the &lt;code&gt;while (i--)&lt;/code&gt; approach as more of a feature of backwards looping than a hack. Perhaps I&#039;ll do a followup to this article to dig into the bytecode generated by each of these loops. It&#039;s quite likely that you&#039;re right about them: the backwards loop is probably doing less work.</description>
		<content:encoded><![CDATA[<p>The JS test is the same as the AS2 and AS3 tests in that all of them use essentially the same loop code. I like to think of the <code>while (i--)</code> approach as more of a feature of backwards looping than a hack. Perhaps I&#8217;ll do a followup to this article to dig into the bytecode generated by each of these loops. It&#8217;s quite likely that you&#8217;re right about them: the backwards loop is probably doing less work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bartosz</title>
		<link>http://jacksondunstan.com/articles/486/comment-page-1#comment-1232</link>
		<dc:creator>Bartosz</dc:creator>
		<pubDate>Mon, 01 Nov 2010 05:41:47 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=486#comment-1232</guid>
		<description>The JS findings aren&#039;t entirely accurate.  Because you are only performing one operation on the reverse while (i--), you are performing half the work the forward loop is doing.

If you change the loops to use the same syntax,
while (i&gt;0) { i--; } You&#039;ll see the performance is exactly the same.  

But yours is a good hack to know if you are just iterating end to end.</description>
		<content:encoded><![CDATA[<p>The JS findings aren&#8217;t entirely accurate.  Because you are only performing one operation on the reverse while (i&#8211;), you are performing half the work the forward loop is doing.</p>
<p>If you change the loops to use the same syntax,<br />
while (i&gt;0) { i&#8211;; } You&#8217;ll see the performance is exactly the same.  </p>
<p>But yours is a good hack to know if you are just iterating end to end.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jackson</title>
		<link>http://jacksondunstan.com/articles/486/comment-page-1#comment-1036</link>
		<dc:creator>jackson</dc:creator>
		<pubDate>Sat, 23 Oct 2010 20:19:37 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=486#comment-1036</guid>
		<description>Hmm, perhaps &lt;code&gt;const&lt;/code&gt; has been improved in a recent version of MXMLC. I&#039;ll look into it and post any findings. Thanks for the tip!</description>
		<content:encoded><![CDATA[<p>Hmm, perhaps <code>const</code> has been improved in a recent version of MXMLC. I&#8217;ll look into it and post any findings. Thanks for the tip!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: skyboy</title>
		<link>http://jacksondunstan.com/articles/486/comment-page-1#comment-1035</link>
		<dc:creator>skyboy</dc:creator>
		<pubDate>Sat, 23 Oct 2010 19:41:02 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=486#comment-1035</guid>
		<description>Though, based on for-loop performance from the two tests I did, it looks like nJIT is doing something. However, from the while loop performance, it looks like my computer started doing another task while testing.

2.703 GHz single core Intel Celeron, 2 GB DDR1 RAM, WinXP Home, 42 running processes (2 instances of Firefox); and the latest stand-alone release player

&lt;pre&gt;   For (forward, const): 767
     For (forward, var): 816
  For (forward, inline): 769
         For (backward): 768
 While (forward, const): 826
   While (forward, var): 761
While (forward, inline): 777
       While (backward): 841


   For (forward, const): 765
     For (forward, var): 838
  For (forward, inline): 766
         For (backward): 770
 While (forward, const): 835
   While (forward, var): 749
While (forward, inline): 833
       While (backward): 794&lt;/pre&gt;

and the modified test:
&lt;pre lang=&quot;actionscript3&quot;&gt;package
{
    import flash.utils.*;

    /**
    *   An app to test the speed of for loops versus while loops
    *   @author Jackson Dunstan
	* 	@editor skyboy
    */
    public class ForVsWhile
    {
        public function ForVsWhile()
        {
            var i:int;
            const NUM_ITERATIONS:int = 100000000;
			var iter:int = NUM_ITERATIONS;
            var beforeTime:int;

            beforeTime = getTimer();
            for (i = 0; i &lt; NUM_ITERATIONS; ++i) { }
            trace(&quot;   For (forward, const): &quot; + (getTimer() - beforeTime));

			beforeTime = getTimer();
            for (i = 0; i &lt; iter; ++i) {}
            trace(&quot;     For (forward, var): &quot; + (getTimer() - beforeTime));

			beforeTime = getTimer();
            for (i = 0; i &lt; 100000000; ++i) {}
            trace(&quot;  For (forward, inline): &quot; + (getTimer() - beforeTime));

            beforeTime = getTimer();
            for (i = NUM_ITERATIONS; i--; ) {}
            trace(&quot;         For (backward): &quot; + (getTimer() - beforeTime));

            beforeTime = getTimer();
            i = 0;
            while (i &lt; NUM_ITERATIONS) {++i; }
            trace(&quot; While (forward, const): &quot; + (getTimer() - beforeTime));

			beforeTime = getTimer();
            i = 0;
            while (i &lt; iter) {++i; }
            trace(&quot;   While (forward, var): &quot; + (getTimer() - beforeTime));

			beforeTime = getTimer();
            i = 0;
            while (i &lt; 100000000) {++i; }
            trace(&quot;While (forward, inline): &quot; + (getTimer() - beforeTime));

            beforeTime = getTimer();
            i = NUM_ITERATIONS;
            while (i--) { }
            trace(&quot;       While (backward): &quot; + (getTimer() - beforeTime));

			trace(&#039;\n&#039;);
        }
    }
}&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Though, based on for-loop performance from the two tests I did, it looks like nJIT is doing something. However, from the while loop performance, it looks like my computer started doing another task while testing.</p>
<p>2.703 GHz single core Intel Celeron, 2 GB DDR1 RAM, WinXP Home, 42 running processes (2 instances of Firefox); and the latest stand-alone release player</p>
<pre>   For (forward, const): 767
     For (forward, var): 816
  For (forward, inline): 769
         For (backward): 768
 While (forward, const): 826
   While (forward, var): 761
While (forward, inline): 777
       While (backward): 841

   For (forward, const): 765
     For (forward, var): 838
  For (forward, inline): 766
         For (backward): 770
 While (forward, const): 835
   While (forward, var): 749
While (forward, inline): 833
       While (backward): 794</pre>
<p>and the modified test:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span><span style="color: #000066; font-weight: bold;">.*;</span>
&nbsp;
    <span style="color: #3f5fbf;">/**
    *   An app to test the speed of for loops versus while loops
    *   @author Jackson Dunstan
    *     @editor skyboy
    */</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ForVsWhile
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ForVsWhile<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000066; font-weight: bold;">;</span>
            const NUM_ITERATIONS<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">100000000</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> iter<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = NUM_ITERATIONS<span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> beforeTime<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            beforeTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> NUM_ITERATIONS<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
            <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span>   For <span style="color: #000000;">&#40;</span>forward<span style="color: #000066; font-weight: bold;">,</span> const<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> beforeTime<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            beforeTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> iter<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
            <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span>     For <span style="color: #000000;">&#40;</span>forward<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #6699cc; font-weight: bold;">var</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> beforeTime<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            beforeTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000; font-weight:bold;">100000000</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
            <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span>  For <span style="color: #000000;">&#40;</span>forward<span style="color: #000066; font-weight: bold;">,</span> inline<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> beforeTime<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            beforeTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = NUM_ITERATIONS<span style="color: #000066; font-weight: bold;">;</span> i<span style="color: #000066; font-weight: bold;">--;</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
            <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span>         For <span style="color: #000000;">&#40;</span>backward<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> beforeTime<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            beforeTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            i = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>i <span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> NUM_ITERATIONS<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#125;</span>
            <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span> While <span style="color: #000000;">&#40;</span>forward<span style="color: #000066; font-weight: bold;">,</span> const<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> beforeTime<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            beforeTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            i = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>i <span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> iter<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#125;</span>
            <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span>   While <span style="color: #000000;">&#40;</span>forward<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #6699cc; font-weight: bold;">var</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> beforeTime<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            beforeTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            i = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>i <span style="color: #000066; font-weight: bold;">&amp;</span>lt<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000; font-weight:bold;">100000000</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000;">&#125;</span>
            <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span>While <span style="color: #000000;">&#40;</span>forward<span style="color: #000066; font-weight: bold;">,</span> inline<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> beforeTime<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            beforeTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            i = NUM_ITERATIONS<span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #0033ff; font-weight: bold;">while</span> <span style="color: #000000;">&#40;</span>i<span style="color: #000066; font-weight: bold;">--</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
            <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span>       While <span style="color: #000000;">&#40;</span>backward<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000066; font-weight: bold;">&amp;</span>quot<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">-</span> beforeTime<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
            <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">&amp;</span>#039<span style="color: #000066; font-weight: bold;">;</span>\n<span style="color: #000066; font-weight: bold;">&amp;</span>#039<span style="color: #000066; font-weight: bold;">;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: jackson</title>
		<link>http://jacksondunstan.com/articles/486/comment-page-1#comment-1026</link>
		<dc:creator>jackson</dc:creator>
		<pubDate>Sat, 23 Oct 2010 04:16:08 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=486#comment-1026</guid>
		<description>I&#039;ve looked at the bytecode generated by MXMLC for &lt;code&gt;var&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; before and never have I seen any differences. It seems to me that the only difference is that there is extra checking at compile time to make sure you don&#039;t reassign the variable when it is specified as &lt;code&gt;const&lt;/code&gt;. That is quite useful, but the optimizations that &lt;code&gt;const&lt;/code&gt; brings along don&#039;t seem to be present in MXMLC, at least of version 4.1. For example, there is no constant folding, even in the case of simple integer &lt;code&gt;const&lt;/code&gt; variables.

So in the case of the loops in the article, it would seem to me that the JIT would, if it&#039;s doing anything, see the literal &lt;code&gt;0&lt;/code&gt; as a constant and not the variable &lt;code&gt;NUM_ITERATIONS&lt;/code&gt;. It seems unlikely that the JIT would see things the other way around and optimize for the variable over the literal.

To me, it&#039;s still a mystery why forward loops run faster than backward loops. My assumption is that since forward loops are by far more common, this area of performance&#8212;on whatever level&#8212;has received more attention from Adobe as it represents a far bigger performance win.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve looked at the bytecode generated by MXMLC for <code>var</code> and <code>const</code> before and never have I seen any differences. It seems to me that the only difference is that there is extra checking at compile time to make sure you don&#8217;t reassign the variable when it is specified as <code>const</code>. That is quite useful, but the optimizations that <code>const</code> brings along don&#8217;t seem to be present in MXMLC, at least of version 4.1. For example, there is no constant folding, even in the case of simple integer <code>const</code> variables.</p>
<p>So in the case of the loops in the article, it would seem to me that the JIT would, if it&#8217;s doing anything, see the literal <code>0</code> as a constant and not the variable <code>NUM_ITERATIONS</code>. It seems unlikely that the JIT would see things the other way around and optimize for the variable over the literal.</p>
<p>To me, it&#8217;s still a mystery why forward loops run faster than backward loops. My assumption is that since forward loops are by far more common, this area of performance&mdash;on whatever level&mdash;has received more attention from Adobe as it represents a far bigger performance win.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: skyboy</title>
		<link>http://jacksondunstan.com/articles/486/comment-page-1#comment-1023</link>
		<dc:creator>skyboy</dc:creator>
		<pubDate>Sat, 23 Oct 2010 00:35:10 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=486#comment-1023</guid>
		<description>I believe I have discovered why the forward loops are faster in AS3: nJIT.

It sees that you&#039;re comparing against a constant, and inlines it, making the effective bytecode:
&lt;pre&gt;getlocal
pushint
ifge jump&lt;/pre&gt;

While the bytecode of the backwards loop is effectively (there may be more to it, I&#039;ve noticed that adding &lt;code&gt; gt 0&lt;/code&gt; can speed it up):
&lt;pre&gt;getlocal
dup
increment
setlocal
iffalse jump&lt;/pre&gt;

And the forwards loop is slower in because you used &lt;code&gt;var&lt;/code&gt; instead of &lt;code&gt;const&lt;/code&gt; making the equive AS3 bytecode:
&lt;pre&gt;getlocal
getlocal
ifgr jump&lt;/pre&gt;

get/set local are slow operations, if you use a var in AS3 you should also see the slowdown as in JS.

The slowdown/speedup in AS2 however is confusing.  I can&#039;t come up with a remotely valid reason for this, or even how to do it on the player&#039;s end.</description>
		<content:encoded><![CDATA[<p>I believe I have discovered why the forward loops are faster in AS3: nJIT.</p>
<p>It sees that you&#8217;re comparing against a constant, and inlines it, making the effective bytecode:</p>
<pre>getlocal
pushint
ifge jump</pre>
<p>While the bytecode of the backwards loop is effectively (there may be more to it, I&#8217;ve noticed that adding <code> gt 0</code> can speed it up):</p>
<pre>getlocal
dup
increment
setlocal
iffalse jump</pre>
<p>And the forwards loop is slower in because you used <code>var</code> instead of <code>const</code> making the equive AS3 bytecode:</p>
<pre>getlocal
getlocal
ifgr jump</pre>
<p>get/set local are slow operations, if you use a var in AS3 you should also see the slowdown as in JS.</p>
<p>The slowdown/speedup in AS2 however is confusing.  I can&#8217;t come up with a remotely valid reason for this, or even how to do it on the player&#8217;s end.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jackson</title>
		<link>http://jacksondunstan.com/articles/486/comment-page-1#comment-415</link>
		<dc:creator>jackson</dc:creator>
		<pubDate>Mon, 30 Nov 2009 18:00:42 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=486#comment-415</guid>
		<description>Thanks for the points!

1. Yes, I think they&#039;re wrong. :) If you look at the bytecodes available in AVM2 you&#039;ll see instructions like &quot;increment_i&quot;, &quot;inclocal_i&quot;, &quot;add_i&quot; and so forth. Adobe puts out a &lt;a href=&quot;http://www.adobe.com/devnet/actionscript/articles/avm2overview.pdf&quot; rel=&quot;nofollow&quot;&gt;good overview&lt;/a&gt;. There&#039;s no mention of &quot;pre&quot; and &quot;post&quot;; that is only an order of bytecode generation by the compiler based on the precedence rules of AS3. In the end it is the same bytecode. Still, that&#039;s something I could have been clearer about in the article.

2. My tests didn&#039;t show any difference between &quot;forward while&quot; and &quot;forward for&quot; for AS3. Do you see any difference when you don&#039;t swap the order around? How about when you use Flex instead of CS4? I&#039;m not seeing any difference when I swap any of the tests around.

3. This is good to know! I wonder what accounts for the mysterious slowness &quot;forward for&quot; on AS2...</description>
		<content:encoded><![CDATA[<p>Thanks for the points!</p>
<p>1. Yes, I think they&#8217;re wrong. :) If you look at the bytecodes available in AVM2 you&#8217;ll see instructions like &#8220;increment_i&#8221;, &#8220;inclocal_i&#8221;, &#8220;add_i&#8221; and so forth. Adobe puts out a <a href="http://www.adobe.com/devnet/actionscript/articles/avm2overview.pdf" rel="nofollow">good overview</a>. There&#8217;s no mention of &#8220;pre&#8221; and &#8220;post&#8221;; that is only an order of bytecode generation by the compiler based on the precedence rules of AS3. In the end it is the same bytecode. Still, that&#8217;s something I could have been clearer about in the article.</p>
<p>2. My tests didn&#8217;t show any difference between &#8220;forward while&#8221; and &#8220;forward for&#8221; for AS3. Do you see any difference when you don&#8217;t swap the order around? How about when you use Flex instead of CS4? I&#8217;m not seeing any difference when I swap any of the tests around.</p>
<p>3. This is good to know! I wonder what accounts for the mysterious slowness &#8220;forward for&#8221; on AS2&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Penner</title>
		<link>http://jacksondunstan.com/articles/486/comment-page-1#comment-414</link>
		<dc:creator>Robert Penner</dc:creator>
		<pubDate>Mon, 30 Nov 2009 17:38:26 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=486#comment-414</guid>
		<description>A few points:
1. Some people say there is a speed difference between pre-increment and post-increment. 

2. When I move the &quot;forward while&quot; code to the top, it runs slower than the &quot;forward for&quot; (AS3, Flash CS4). It often seems that code running first has a disadvantage.

3. I decompiled the loops; the for and while loops come back exactly the same (AS2 and AS3).</description>
		<content:encoded><![CDATA[<p>A few points:<br />
1. Some people say there is a speed difference between pre-increment and post-increment. </p>
<p>2. When I move the &#8220;forward while&#8221; code to the top, it runs slower than the &#8220;forward for&#8221; (AS3, Flash CS4). It often seems that code running first has a disadvantage.</p>
<p>3. I decompiled the loops; the for and while loops come back exactly the same (AS2 and AS3).</p>
]]></content:encoded>
	</item>
</channel>
</rss>

