<?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: Improving the Limit Function</title>
	<atom:link href="http://www.pixelwit.com/blog/2007/06/improving-the-limit-function/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pixelwit.com/blog/2007/06/improving-the-limit-function/</link>
	<description>Making Flash Go</description>
	<lastBuildDate>Fri, 03 Feb 2012 01:38:11 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Pixelwit</title>
		<link>http://www.pixelwit.com/blog/2007/06/improving-the-limit-function/comment-page-1/#comment-7848</link>
		<dc:creator>Pixelwit</dc:creator>
		<pubDate>Tue, 15 Jan 2008 02:50:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.pixelwit.com/blog/2007/06/04/improving-the-limit-function/#comment-7848</guid>
		<description>Hi Joga, although using an array provides an easy method to sort the limiters, I think the array notation (i.e. limits[0]) decreases performance.  I ran a quick performance test and your code seemed about 85% slower than the code I provided.  

Flash has to look-up a variable twice with array notation, once to find &quot;limits&quot;, and then another time to find index &quot;0&quot; within &quot;limits&quot;.  In contrast, a local variable like &quot;num&quot; only needs to be looked-up once.  In AS1 and AS2 looking-up variables can be a fairly slow process, I&#039;m unsure about AS3.</description>
		<content:encoded><![CDATA[<p>Hi Joga, although using an array provides an easy method to sort the limiters, I think the array notation (i.e. limits[0]) decreases performance.  I ran a quick performance test and your code seemed about 85% slower than the code I provided.  </p>
<p>Flash has to look-up a variable twice with array notation, once to find &#8220;limits&#8221;, and then another time to find index &#8220;0&#8243; within &#8220;limits&#8221;.  In contrast, a local variable like &#8220;num&#8221; only needs to be looked-up once.  In AS1 and AS2 looking-up variables can be a fairly slow process, I&#8217;m unsure about AS3.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joga Luce</title>
		<link>http://www.pixelwit.com/blog/2007/06/improving-the-limit-function/comment-page-1/#comment-7834</link>
		<dc:creator>Joga Luce</dc:creator>
		<pubDate>Mon, 14 Jan 2008 17:53:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.pixelwit.com/blog/2007/06/04/improving-the-limit-function/#comment-7834</guid>
		<description>what about simple array sorting. maybe i’m wrong, i’m still new to flash.

&lt;code lang=&quot;actionscript&quot;&gt;
function limit(num:Number,limits:Array){
	limits.sort();
	return (num &lt; limits[0]) ? limits[0] : (num &gt; limits[1]) ? limits[1] : num;
}

function limitReadable(num:Number, limits:Array){
	limits.sort();
	if(num &lt; limits[0]) {
		return limits[0];
	}
	if(num &gt; limits[1]) {
		return limits[1];
	}
	return num;
}
&lt;/code&gt;

</description>
		<content:encoded><![CDATA[<p>what about simple array sorting. maybe i’m wrong, i’m still new to flash.</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> limit<span style="color: #66cc66;">&#40;</span>num:<span style="color: #0066CC;">Number</span>,limits:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; limits.<span style="color: #0066CC;">sort</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>num <span style="color: #66cc66;">&amp;</span>lt; limits<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> ? limits<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> : <span style="color: #66cc66;">&#40;</span>num <span style="color: #66cc66;">&amp;</span>gt; limits<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> ? limits<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> : num;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> limitReadable<span style="color: #66cc66;">&#40;</span>num:<span style="color: #0066CC;">Number</span>, limits:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; limits.<span style="color: #0066CC;">sort</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>num <span style="color: #66cc66;">&amp;</span>lt; limits<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> limits<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>num <span style="color: #66cc66;">&amp;</span>gt; limits<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> limits<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> num;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
]]></content:encoded>
	</item>
	<item>
		<title>By: shaun</title>
		<link>http://www.pixelwit.com/blog/2007/06/improving-the-limit-function/comment-page-1/#comment-186</link>
		<dc:creator>shaun</dc:creator>
		<pubDate>Tue, 05 Jun 2007 14:06:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.pixelwit.com/blog/2007/06/04/improving-the-limit-function/#comment-186</guid>
		<description>Ah! good to know :) In some languages it is more costly to have complicated &quot;if&quot; forking than simple variable declaration - has something to do with the processor being able to predict likely paths through the code - but that&#039;s obviously not the case with actionScript. Also, your &quot;early out&quot; returns probably help in a lot of cases :)</description>
		<content:encoded><![CDATA[<p>Ah! good to know <img src='http://www.pixelwit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  In some languages it is more costly to have complicated &#8220;if&#8221; forking than simple variable declaration &#8211; has something to do with the processor being able to predict likely paths through the code &#8211; but that&#8217;s obviously not the case with actionScript. Also, your &#8220;early out&#8221; returns probably help in a lot of cases <img src='http://www.pixelwit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pixelwit</title>
		<link>http://www.pixelwit.com/blog/2007/06/improving-the-limit-function/comment-page-1/#comment-177</link>
		<dc:creator>Pixelwit</dc:creator>
		<pubDate>Mon, 04 Jun 2007 21:47:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.pixelwit.com/blog/2007/06/04/improving-the-limit-function/#comment-177</guid>
		<description>Hi Shaun,

I originally went the same way as you did with your code, but I decided to use the approach listed above for performance reasons since I frequently use the code in loops which iterate through a large number of objects.

The main thing that slows down the code you provided is the creation of the &quot;tmp&quot; variable, which in my tests was only a tiny bit slower, most notably when &quot;hi&quot; was less than &quot;lo&quot;.

Thanks for your suggestion, interest and perseverance.  :D</description>
		<content:encoded><![CDATA[<p>Hi Shaun,</p>
<p>I originally went the same way as you did with your code, but I decided to use the approach listed above for performance reasons since I frequently use the code in loops which iterate through a large number of objects.</p>
<p>The main thing that slows down the code you provided is the creation of the &#8220;tmp&#8221; variable, which in my tests was only a tiny bit slower, most notably when &#8220;hi&#8221; was less than &#8220;lo&#8221;.</p>
<p>Thanks for your suggestion, interest and perseverance.  <img src='http://www.pixelwit.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shaun</title>
		<link>http://www.pixelwit.com/blog/2007/06/improving-the-limit-function/comment-page-1/#comment-176</link>
		<dc:creator>shaun</dc:creator>
		<pubDate>Mon, 04 Jun 2007 20:55:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.pixelwit.com/blog/2007/06/04/improving-the-limit-function/#comment-176</guid>
		<description>How about switching hi and lo if they are in the wrong order? Like such:

&lt;code lang=&quot;actionscript&quot;&gt;
function limit(num, lo, hi) {
  if (hi &lt; lo) {
    var tmp = hi;
    hi = lo;
    lo = tmp;
  }
  if (num &lt; lo) return lo;
  if (num &gt; hi) return hi;
  return num;
}
&lt;/code&gt;

</description>
		<content:encoded><![CDATA[<p>How about switching hi and lo if they are in the wrong order? Like such:</p>
<div class="codecolorer-container actionscript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> limit<span style="color: #66cc66;">&#40;</span>num, lo, hi<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>hi <span style="color: #66cc66;">&amp;</span>lt; lo<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> tmp = hi;<br />
&nbsp; &nbsp; hi = lo;<br />
&nbsp; &nbsp; lo = tmp;<br />
&nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>num <span style="color: #66cc66;">&amp;</span>lt; lo<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> lo;<br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>num <span style="color: #66cc66;">&amp;</span>gt; hi<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">return</span> hi;<br />
&nbsp; <span style="color: #b1b100;">return</span> num;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
]]></content:encoded>
	</item>
</channel>
</rss>

