<?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: Unroll your loops!</title>
	<atom:link href="http://blog.iconara.net/2008/09/07/unroll-your-loops/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.iconara.net/2008/09/07/unroll-your-loops/</link>
	<description></description>
	<lastBuildDate>Mon, 07 Nov 2011 12:03:53 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Theo</title>
		<link>http://blog.iconara.net/2008/09/07/unroll-your-loops/comment-page-1/#comment-7596</link>
		<dc:creator>Theo</dc:creator>
		<pubDate>Sun, 21 Sep 2008 09:30:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.iconara.net/?p=296#comment-7596</guid>
		<description>&lt;p&gt;The lesson here is that you shouldn&#039;t optimize while you refactor.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>The lesson here is that you shouldn&#8217;t optimize while you refactor.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Severin</title>
		<link>http://blog.iconara.net/2008/09/07/unroll-your-loops/comment-page-1/#comment-7595</link>
		<dc:creator>Severin</dc:creator>
		<pubDate>Sun, 21 Sep 2008 01:37:07 +0000</pubDate>
		<guid isPermaLink="false">http://blog.iconara.net/?p=296#comment-7595</guid>
		<description>&lt;p&gt;Well, I&#039;d say your guts were right, at least if this code is really a performance bottleneck: if you&#039;d do let&#039;s say a 1000000 iterations on doTheThing(), then the first thing would be substantially faster done, at least if there&#039;s only a little bunch of items to iterate locally. Why? Because method calls in AS3 are much slower (I&#039;m talking of factor 100) than assignments. But I agree, that - if you wouldn&#039;t have to do the thing many, many times - there&#039;s only a theoratical difference. Ehm, hope my 2 dirty cents made themselves kind of clear ;)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Well, I&#8217;d say your guts were right, at least if this code is really a performance bottleneck: if you&#8217;d do let&#8217;s say a 1000000 iterations on doTheThing(), then the first thing would be substantially faster done, at least if there&#8217;s only a little bunch of items to iterate locally. Why? Because method calls in AS3 are much slower (I&#8217;m talking of factor 100) than assignments. But I agree, that &#8211; if you wouldn&#8217;t have to do the thing many, many times &#8211; there&#8217;s only a theoratical difference. Ehm, hope my 2 dirty cents made themselves kind of clear ;)</p>]]></content:encoded>
	</item>
	<item>
		<title>By: bjorn</title>
		<link>http://blog.iconara.net/2008/09/07/unroll-your-loops/comment-page-1/#comment-7561</link>
		<dc:creator>bjorn</dc:creator>
		<pubDate>Mon, 08 Sep 2008 06:51:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.iconara.net/?p=296#comment-7561</guid>
		<description>&lt;p&gt;Good one Theo, my gut told me the same thing yours did .. but when thinking about it I can&#039;t see why I thought setting up a loop should be so costly :-)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Good one Theo, my gut told me the same thing yours did .. but when thinking about it I can&#8217;t see why I thought setting up a loop should be so costly :-)</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Theo</title>
		<link>http://blog.iconara.net/2008/09/07/unroll-your-loops/comment-page-1/#comment-7560</link>
		<dc:creator>Theo</dc:creator>
		<pubDate>Mon, 08 Sep 2008 05:42:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.iconara.net/?p=296#comment-7560</guid>
		<description>&lt;p&gt;You&#039;re right of course, but it&#039;s also a very different example. The point of mine was that the loop body wasn&#039;t interdependent. In your example the parts don&#039;t do half the work. It&#039;s less likely that you&#039;d want to refactor a piece of code that worked like that because the parts depend on each other. I&#039;m not claiming that any loop can be split apart and maintain the same time complexity.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>You&#8217;re right of course, but it&#8217;s also a very different example. The point of mine was that the loop body wasn&#8217;t interdependent. In your example the parts don&#8217;t do half the work. It&#8217;s less likely that you&#8217;d want to refactor a piece of code that worked like that because the parts depend on each other. I&#8217;m not claiming that any loop can be split apart and maintain the same time complexity.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: ronen</title>
		<link>http://blog.iconara.net/2008/09/07/unroll-your-loops/comment-page-1/#comment-7558</link>
		<dc:creator>ronen</dc:creator>
		<pubDate>Sun, 07 Sep 2008 21:17:09 +0000</pubDate>
		<guid isPermaLink="false">http://blog.iconara.net/?p=296#comment-7558</guid>
		<description>&lt;p&gt;Its all a matter of what is the operation on which your measuring the runtime (summation and max operations in your case), lets imagine a case where you have method calcA and calcB, each cost o(1) used in the following loop&lt;/p&gt;

&lt;p&gt;int acc=0,temp=0;
for(int i=0;i&lt;n;i++){
  temp+=calcA(i); 
  acc+=calcB(temp);
}&lt;/p&gt;

&lt;p&gt;The run time is o((1+1)n)=o(n)&lt;/p&gt;

&lt;p&gt;Now lets unroll this loop into two separate loops:&lt;/p&gt;

&lt;p&gt;int methodA(int k){
  int temp=0;
   for(int j=0;j&lt;=k;j++){
    temp+=calcA(j); 
   }&lt;br /&gt;
  return temp;
}&lt;/p&gt;

&lt;p&gt;int methodB(int n){
  int acc=0;
   for(int i=0;i&lt;n;i++){
    acc+=calcB(methodA(i)); 
   }&lt;br /&gt;
  return acc;
}&lt;/p&gt;

&lt;p&gt;for each calcB we calcA i times, which makes the runtime:
  ((1+1) + (1+2) + .. + (1+n-1))=(2+3+..+n) = (n (2 + n)) / 2 = (2n+n^2)/2 = o(n^2)
This is quadratic time instead of linear time!
Now for simple un-rolling its visible and can be prevented by a worthy programmer the problem is that in real life code isn&#039;t as clear as in this case, still you are correct since usually most of the code doesn&#039;t reside within the 10% in which the program spends 90% of its processing time (therefor making optimizations worthless).&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Its all a matter of what is the operation on which your measuring the runtime (summation and max operations in your case), lets imagine a case where you have method calcA and calcB, each cost o(1) used in the following loop</p>

<p>int acc=0,temp=0;
for(int i=0;i&lt;n;i++){
  temp+=calcA(i); 
  acc+=calcB(temp);
}</p>

<p>The run time is o((1+1)n)=o(n)</p>

<p>Now lets unroll this loop into two separate loops:</p>

<p>int methodA(int k){
  int temp=0;
   for(int j=0;j&lt;=k;j++){
    temp+=calcA(j); 
   }<br />
  return temp;
}</p>

<p>int methodB(int n){
  int acc=0;
   for(int i=0;i&lt;n;i++){
    acc+=calcB(methodA(i)); 
   }<br />
  return acc;
}</p>

<p>for each calcB we calcA i times, which makes the runtime:
  ((1+1) + (1+2) + .. + (1+n-1))=(2+3+..+n) = (n (2 + n)) / 2 = (2n+n^2)/2 = o(n^2)
This is quadratic time instead of linear time!
Now for simple un-rolling its visible and can be prevented by a worthy programmer the problem is that in real life code isn&#8217;t as clear as in this case, still you are correct since usually most of the code doesn&#8217;t reside within the 10% in which the program spends 90% of its processing time (therefor making optimizations worthless).</p>]]></content:encoded>
	</item>
</channel>
</rss>

