<?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: Avoid ints in ActionScript</title>
	<atom:link href="http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/</link>
	<description>music, technology, interfaces, people</description>
	<lastBuildDate>Mon, 19 Oct 2009 16:37:14 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Sho</title>
		<link>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/comment-page-1/#comment-293586</link>
		<dc:creator>Sho</dc:creator>
		<pubDate>Thu, 17 Sep 2009 23:55:34 +0000</pubDate>
		<guid isPermaLink="false">http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/#comment-293586</guid>
		<description>Hi Steven. Good point!

The issue is that in many languages (such as C++ or Java), (j+15)/7 *is* an integer operation when j is an int. So for those of us who come from those languages (and who see the strong typing in AS3 as a move toward those languages), this is confusing.

Even if you argue that having this be a floating point operation is &#039;better&#039; than having it be an integer operation, a modern compiler should be able to analyze the code to discover that the result of this operation is stored in an int and do an integer version of the division operation.

If I remember correctly, the player team investigated this approach, and found that it was difficult to get strictly correct behavior in edge cases (like dividing by NaN, etc). As a trade-off, they did an intrinsic conversion to Number in many cases that aren&#039;t strictly necessary.

They may have improved this in more recent version of the VM. I haven&#039;t worked at Adobe for 2 years, so I don&#039;t really know what&#039;s going on these days.</description>
		<content:encoded><![CDATA[<p>Hi Steven. Good point!</p>
<p>The issue is that in many languages (such as C++ or Java), (j+15)/7 *is* an integer operation when j is an int. So for those of us who come from those languages (and who see the strong typing in AS3 as a move toward those languages), this is confusing.</p>
<p>Even if you argue that having this be a floating point operation is &#8216;better&#8217; than having it be an integer operation, a modern compiler should be able to analyze the code to discover that the result of this operation is stored in an int and do an integer version of the division operation.</p>
<p>If I remember correctly, the player team investigated this approach, and found that it was difficult to get strictly correct behavior in edge cases (like dividing by NaN, etc). As a trade-off, they did an intrinsic conversion to Number in many cases that aren&#8217;t strictly necessary.</p>
<p>They may have improved this in more recent version of the VM. I haven&#8217;t worked at Adobe for 2 years, so I don&#8217;t really know what&#8217;s going on these days.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steven</title>
		<link>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/comment-page-1/#comment-293583</link>
		<dc:creator>Steven</dc:creator>
		<pubDate>Thu, 17 Sep 2009 23:36:18 +0000</pubDate>
		<guid isPermaLink="false">http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/#comment-293583</guid>
		<description>Sho, 

Your blanket statement that Number is better than int is not fair at all.

Your loop is doing an int() cast every single iteration.  Of course this is going to be slow.  Note the difference if you don&#039;t divide by 7 and it doesn&#039;t have to do a conversion.

var now:int = getTimer();
var j:int;
var i:int = 10000000;
while (i--)
{
    j = (j + 15);
}
trace(getTimer() - now);

41ms

If you divide by 7

154ms

Obviously, doing floating point math using Number is faster than doing it with int.  But doing integer math with int is MUCH faster than doing it with Number.

If you&#039;re using floats, use Number. If you&#039;re using integers, use int.</description>
		<content:encoded><![CDATA[<p>Sho, </p>
<p>Your blanket statement that Number is better than int is not fair at all.</p>
<p>Your loop is doing an int() cast every single iteration.  Of course this is going to be slow.  Note the difference if you don&#8217;t divide by 7 and it doesn&#8217;t have to do a conversion.</p>
<p>var now:int = getTimer();<br />
var j:int;<br />
var i:int = 10000000;<br />
while (i&#8211;)<br />
{<br />
    j = (j + 15);<br />
}<br />
trace(getTimer() &#8211; now);</p>
<p>41ms</p>
<p>If you divide by 7</p>
<p>154ms</p>
<p>Obviously, doing floating point math using Number is faster than doing it with int.  But doing integer math with int is MUCH faster than doing it with Number.</p>
<p>If you&#8217;re using floats, use Number. If you&#8217;re using integers, use int.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steven</title>
		<link>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/comment-page-1/#comment-293579</link>
		<dc:creator>Steven</dc:creator>
		<pubDate>Thu, 17 Sep 2009 21:42:06 +0000</pubDate>
		<guid isPermaLink="false">http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/#comment-293579</guid>
		<description>http://lab.polygonal.de/2007/06/06/int-uint-and-number-data-type-conversion/</description>
		<content:encoded><![CDATA[<p><a href="http://lab.polygonal.de/2007/06/06/int-uint-and-number-data-type-conversion/" rel="nofollow">http://lab.polygonal.de/2007/06/06/int-uint-and-number-data-type-conversion/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: [flash] Variables and Data types &#171; Not just a note</title>
		<link>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/comment-page-1/#comment-293379</link>
		<dc:creator>[flash] Variables and Data types &#171; Not just a note</dc:creator>
		<pubDate>Mon, 14 Sep 2009 17:05:35 +0000</pubDate>
		<guid isPermaLink="false">http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/#comment-293379</guid>
		<description>[...] / Uint * Only works in whole numbers. &lt;Some said The Number works faster, see here, or [...]</description>
		<content:encoded><![CDATA[<p>[...] / Uint * Only works in whole numbers. &lt;Some said The Number works faster, see here, or [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Actionscript optimized types and why as3&#8217;s int sucks ass at tec.</title>
		<link>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/comment-page-1/#comment-278346</link>
		<dc:creator>Actionscript optimized types and why as3&#8217;s int sucks ass at tec.</dc:creator>
		<pubDate>Wed, 06 May 2009 14:24:52 +0000</pubDate>
		<guid isPermaLink="false">http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/#comment-278346</guid>
		<description>[...] They writer of this blog actually suggests to default to Number instead of int, because Numbers are faster in most cases avoid ints in Actionscript [...]</description>
		<content:encoded><![CDATA[<p>[...] They writer of this blog actually suggests to default to Number instead of int, because Numbers are faster in most cases avoid ints in Actionscript [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: The logical consequences that never happened at controul</title>
		<link>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/comment-page-1/#comment-265148</link>
		<dc:creator>The logical consequences that never happened at controul</dc:creator>
		<pubDate>Sun, 22 Mar 2009 17:32:27 +0000</pubDate>
		<guid isPermaLink="false">http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/#comment-265148</guid>
		<description>[...] a BETTER one. Function inlining, mixing as3 and bytecode, optimised integer math (I do NOT want to work with Numbers); the list of possible enhancements was long. Why would anyone really need any of this? Mainly [...]</description>
		<content:encoded><![CDATA[<p>[...] a BETTER one. Function inlining, mixing as3 and bytecode, optimised integer math (I do NOT want to work with Numbers); the list of possible enhancements was long. Why would anyone really need any of this? Mainly [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sho</title>
		<link>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/comment-page-1/#comment-257088</link>
		<dc:creator>Sho</dc:creator>
		<pubDate>Mon, 02 Mar 2009 18:24:04 +0000</pubDate>
		<guid isPermaLink="false">http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/#comment-257088</guid>
		<description>To Jacob and Drawtree:

Thanks for engaging in this discusssion!

Not sure what the timings are these days with player 10, but (a) you&#039;re right in the narrow sense, and (b) I still think there is an efficiency issue in the broader sense.

Jacob&#039;s point (and drawtree&#039;s, implicitly) is that certain operations are faster when done as ints. That is undoubtedly true.

However, for those of us who come from a non-dynamic language background (C / C++ / Java), the expectation is that built-in integral types are there precisely for the purposes of being faster than generic number types, even when doing things like printing them or dividing them.

In many other languages, dividing two integers compiles down to an integer division operation which is much much faster than a floating point integer division operation. In ActionScript (as of player 9, anyway), the integers are converted to number before the division. And as for the print statement specified in Gary&#039;s excellent talk, it&#039;s a mystery to me why the conversion occurs. Perhaps because the signature of the print method takes an untyped argument?

So... to summarize... I believe that the performance behavior of ints in ActionScript would be surprising to many people. Dividing two ints would normally be faster than dividing two floating point numbers, for example.

As for the title of the post, I may have been guilty of engaging in hyperbole. Maybe it should have been called &quot;consider using Number instead of int inside of tight loops&quot;. I certainly continue to use ints on a daily basis for typechecking purposes.</description>
		<content:encoded><![CDATA[<p>To Jacob and Drawtree:</p>
<p>Thanks for engaging in this discusssion!</p>
<p>Not sure what the timings are these days with player 10, but (a) you&#8217;re right in the narrow sense, and (b) I still think there is an efficiency issue in the broader sense.</p>
<p>Jacob&#8217;s point (and drawtree&#8217;s, implicitly) is that certain operations are faster when done as ints. That is undoubtedly true.</p>
<p>However, for those of us who come from a non-dynamic language background (C / C++ / Java), the expectation is that built-in integral types are there precisely for the purposes of being faster than generic number types, even when doing things like printing them or dividing them.</p>
<p>In many other languages, dividing two integers compiles down to an integer division operation which is much much faster than a floating point integer division operation. In ActionScript (as of player 9, anyway), the integers are converted to number before the division. And as for the print statement specified in Gary&#8217;s excellent talk, it&#8217;s a mystery to me why the conversion occurs. Perhaps because the signature of the print method takes an untyped argument?</p>
<p>So&#8230; to summarize&#8230; I believe that the performance behavior of ints in ActionScript would be surprising to many people. Dividing two ints would normally be faster than dividing two floating point numbers, for example.</p>
<p>As for the title of the post, I may have been guilty of engaging in hyperbole. Maybe it should have been called &#8220;consider using Number instead of int inside of tight loops&#8221;. I certainly continue to use ints on a daily basis for typechecking purposes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: drawtree</title>
		<link>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/comment-page-1/#comment-257075</link>
		<dc:creator>drawtree</dc:creator>
		<pubDate>Mon, 02 Mar 2009 15:03:57 +0000</pubDate>
		<guid isPermaLink="false">http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/#comment-257075</guid>
		<description>The title and conclusion of this post is wrong. Jacob is right. Your code has serious logical bug, and it makes your conclusion wrong.

Refer 25p. of this document:
http://www.onflex.org/ACDS/AS3TuningInsideAVM2JIT.pdf

If you understand concept of explicit-type-casting you&#039;re already know what I want to say.</description>
		<content:encoded><![CDATA[<p>The title and conclusion of this post is wrong. Jacob is right. Your code has serious logical bug, and it makes your conclusion wrong.</p>
<p>Refer 25p. of this document:<br />
<a href="http://www.onflex.org/ACDS/AS3TuningInsideAVM2JIT.pdf" rel="nofollow">http://www.onflex.org/ACDS/AS3TuningInsideAVM2JIT.pdf</a></p>
<p>If you understand concept of explicit-type-casting you&#8217;re already know what I want to say.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: int vs Number &#171; Pushing the boundaries</title>
		<link>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/comment-page-1/#comment-11600</link>
		<dc:creator>int vs Number &#171; Pushing the boundaries</dc:creator>
		<pubDate>Sun, 11 Feb 2007 09:10:47 +0000</pubDate>
		<guid isPermaLink="false">http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/#comment-11600</guid>
		<description>[...] Posted by vijayram on February 1st, 2007  This issue was highlighted by Sho Kuwamoto sometime back who recommends to use Number where ever possible. Grant Skinner made some additional test and has posted his results here. [...]</description>
		<content:encoded><![CDATA[<p>[...] Posted by vijayram on February 1st, 2007  This issue was highlighted by Sho Kuwamoto sometime back who recommends to use Number where ever possible. Grant Skinner made some additional test and has posted his results here. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jacob Correia</title>
		<link>http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/comment-page-1/#comment-8470</link>
		<dc:creator>Jacob Correia</dc:creator>
		<pubDate>Sat, 06 Jan 2007 00:50:56 +0000</pubDate>
		<guid isPermaLink="false">http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/#comment-8470</guid>
		<description>You should use int for your loops and any other values that will not have decimal places. Using an int as a loop iterator is faster than using a number.
 
Once you start using calculations that add decimal places to the int(like division/multiplication or adding non integer numbers) then you get a slowdown since the result basically has to be rounded. This explains your results above. 

Even so, the conversion is pretty fast. Change m = ((m + 15) / 7) to m = Math.round((m + 15) / 7) to simulate int rounding and you&#039;ll see using an int seems faster than using Math.round() on a number.</description>
		<content:encoded><![CDATA[<p>You should use int for your loops and any other values that will not have decimal places. Using an int as a loop iterator is faster than using a number.</p>
<p>Once you start using calculations that add decimal places to the int(like division/multiplication or adding non integer numbers) then you get a slowdown since the result basically has to be rounded. This explains your results above. </p>
<p>Even so, the conversion is pretty fast. Change m = ((m + 15) / 7) to m = Math.round((m + 15) / 7) to simulate int rounding and you&#8217;ll see using an int seems faster than using Math.round() on a number.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
