<?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: Big Code, Good Code!</title>
	<atom:link href="http://www.mymindleaks.com/blog/archives/big-code-good-code.html/feed" rel="self" type="application/rss+xml" />
	<link>http://www.mymindleaks.com/blog/archives/big-code-good-code.html</link>
	<description>Software, Programming, Architecture &#38; More</description>
	<lastBuildDate>Mon, 26 Jul 2010 06:14:45 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
	<item>
		<title>By: casino online con</title>
		<link>http://www.mymindleaks.com/blog/archives/big-code-good-code.html/comment-page-1#comment-1131</link>
		<dc:creator>casino online con</dc:creator>
		<pubDate>Mon, 14 Jul 2008 08:09:14 +0000</pubDate>
		<guid isPermaLink="false">http://mymindleaks.com/?p=622#comment-1131</guid>
		<description>&lt;strong&gt;casino online con...&lt;/strong&gt;

Recientemente sistemi per roulette casino live gambling game gratis giocare a poker online free craps...</description>
		<content:encoded><![CDATA[<p><strong>casino online con&#8230;</strong></p>
<p>Recientemente sistemi per roulette casino live gambling game gratis giocare a poker online free craps&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Mills</title>
		<link>http://www.mymindleaks.com/blog/archives/big-code-good-code.html/comment-page-1#comment-1003</link>
		<dc:creator>David Mills</dc:creator>
		<pubDate>Sun, 11 May 2008 23:48:31 +0000</pubDate>
		<guid isPermaLink="false">http://mymindleaks.com/?p=622#comment-1003</guid>
		<description>Some even use good grammar.  There&#039;s probably some relationship there, but I won&#039;t pursue it.</description>
		<content:encoded><![CDATA[<p>Some even use good grammar.  There&#8217;s probably some relationship there, but I won&#8217;t pursue it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steendor</title>
		<link>http://www.mymindleaks.com/blog/archives/big-code-good-code.html/comment-page-1#comment-999</link>
		<dc:creator>Steendor</dc:creator>
		<pubDate>Sat, 10 May 2008 16:59:46 +0000</pubDate>
		<guid isPermaLink="false">http://mymindleaks.com/?p=622#comment-999</guid>
		<description>In the same vein, the first test for &#039;k == 1 &amp;&amp; residue == 0&#039; is slightly redundant.  If k == 1, then residue == 0 is always true.

I also feel that I should mention something else.  The &quot;optimalness&quot; of your boundary-checking depends on the values of n and k that you test with.  If your conditions of k == 1 and residue == 0 are frequently true, then you&#039;ll probably see a good speedup.  However, if both of those conditions are frequently false (allow n and k to vary over all positive integers, and see how often they&#039;re true), then your solution has become sub-optimal, as you&#039;re now checking for conditions that are rare.  The bottom line is that checking for boundary cases only saves time if they occur frequently, and if the cost of checking for them is less than the cost of simply using the formula.</description>
		<content:encoded><![CDATA[<p>In the same vein, the first test for &#8216;k == 1 &amp;&amp; residue == 0&#8242; is slightly redundant.  If k == 1, then residue == 0 is always true.</p>
<p>I also feel that I should mention something else.  The &#8220;optimalness&#8221; of your boundary-checking depends on the values of n and k that you test with.  If your conditions of k == 1 and residue == 0 are frequently true, then you&#8217;ll probably see a good speedup.  However, if both of those conditions are frequently false (allow n and k to vary over all positive integers, and see how often they&#8217;re true), then your solution has become sub-optimal, as you&#8217;re now checking for conditions that are rare.  The bottom line is that checking for boundary cases only saves time if they occur frequently, and if the cost of checking for them is less than the cost of simply using the formula.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steendor</title>
		<link>http://www.mymindleaks.com/blog/archives/big-code-good-code.html/comment-page-1#comment-995</link>
		<dc:creator>Steendor</dc:creator>
		<pubDate>Sat, 10 May 2008 06:17:21 +0000</pubDate>
		<guid isPermaLink="false">http://mymindleaks.com/?p=622#comment-995</guid>
		<description>The else-if test for &#039;residue == k&#039; in your &quot;more optimised&quot; solution is unnecessary, as it will never be true.</description>
		<content:encoded><![CDATA[<p>The else-if test for &#8216;residue == k&#8217; in your &#8220;more optimised&#8221; solution is unnecessary, as it will never be true.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: maheshexp</title>
		<link>http://www.mymindleaks.com/blog/archives/big-code-good-code.html/comment-page-1#comment-994</link>
		<dc:creator>maheshexp</dc:creator>
		<pubDate>Sat, 10 May 2008 05:32:22 +0000</pubDate>
		<guid isPermaLink="false">http://mymindleaks.com/?p=622#comment-994</guid>
		<description>@rpgfan, a &#039;CMP&#039; statement would be created in the assembly. Which makes the comparision. if the 1st condition fails, a BRANCH assembly statement would also be executed, which is another costlier statement .</description>
		<content:encoded><![CDATA[<p>@rpgfan, a &#8216;CMP&#8217; statement would be created in the assembly. Which makes the comparision. if the 1st condition fails, a BRANCH assembly statement would also be executed, which is another costlier statement .</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mathmagician</title>
		<link>http://www.mymindleaks.com/blog/archives/big-code-good-code.html/comment-page-1#comment-986</link>
		<dc:creator>Mathmagician</dc:creator>
		<pubDate>Fri, 09 May 2008 15:16:51 +0000</pubDate>
		<guid isPermaLink="false">http://mymindleaks.com/?p=622#comment-986</guid>
		<description>Eh you write &quot;But wait, is your code is faster to run for N &gt; 1^10, K &gt; 1^5&quot;. In my book 1^10  = 1 and 1^5 is 1. I think you meant to write:
N &gt; 10^10, K &gt; 10^5</description>
		<content:encoded><![CDATA[<p>Eh you write &#8220;But wait, is your code is faster to run for N &gt; 1^10, K &gt; 1^5&#8243;. In my book 1^10  = 1 and 1^5 is 1. I think you meant to write:<br />
N &gt; 10^10, K &gt; 10^5</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rpgfan3233</title>
		<link>http://www.mymindleaks.com/blog/archives/big-code-good-code.html/comment-page-1#comment-985</link>
		<dc:creator>rpgfan3233</dc:creator>
		<pubDate>Fri, 09 May 2008 15:04:46 +0000</pubDate>
		<guid isPermaLink="false">http://mymindleaks.com/?p=622#comment-985</guid>
		<description>The bit about (a &gt; b ? c : d) makes no sense. Why does it check a&lt;b&gt;b? All it should expand to is:
if (a &gt; b)
  //do code section &#039;c&#039;
else
  //do code section &#039;d&#039;

I&#039;m not saying you&#039;re wrong, but a decent compiler should optimize that to exactly what you program, not insert a bunch of code that you have no use for.

Maybe Java and possibly even .NET has this issue (it may even be in the specs for the language(s) to do extra boundary checking because of the nature of the language(s)), but I haven&#039;t found any issue with this in C++. I look at the Assembly code output by the compiler, and things are the same. I&#039;m glad I don&#039;t suffer from this issue in C++. It does exactly what I write rather than what it thinks I write. ^_^&lt;/b&gt;</description>
		<content:encoded><![CDATA[<p>The bit about (a &gt; b ? c : d) makes no sense. Why does it check a<b>b? All it should expand to is:<br />
if (a &gt; b)<br />
  //do code section &#8216;c&#8217;<br />
else<br />
  //do code section &#8216;d&#8217;</p>
<p>I&#8217;m not saying you&#8217;re wrong, but a decent compiler should optimize that to exactly what you program, not insert a bunch of code that you have no use for.</p>
<p>Maybe Java and possibly even .NET has this issue (it may even be in the specs for the language(s) to do extra boundary checking because of the nature of the language(s)), but I haven&#8217;t found any issue with this in C++. I look at the Assembly code output by the compiler, and things are the same. I&#8217;m glad I don&#8217;t suffer from this issue in C++. It does exactly what I write rather than what it thinks I write. ^_^</b></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: maheshexp</title>
		<link>http://www.mymindleaks.com/blog/archives/big-code-good-code.html/comment-page-1#comment-984</link>
		<dc:creator>maheshexp</dc:creator>
		<pubDate>Fri, 09 May 2008 14:48:13 +0000</pubDate>
		<guid isPermaLink="false">http://mymindleaks.com/?p=622#comment-984</guid>
		<description>@Travers,

Also your points on improving the slow code is great!</description>
		<content:encoded><![CDATA[<p>@Travers,</p>
<p>Also your points on improving the slow code is great!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: maheshexp</title>
		<link>http://www.mymindleaks.com/blog/archives/big-code-good-code.html/comment-page-1#comment-983</link>
		<dc:creator>maheshexp</dc:creator>
		<pubDate>Fri, 09 May 2008 14:47:04 +0000</pubDate>
		<guid isPermaLink="false">http://mymindleaks.com/?p=622#comment-983</guid>
		<description>@Bjarne, that was a silly mistake. I&#039;ve corrected it. Thanks for pointing it. I&#039;ve moved the &quot;else if&quot; as &quot;if&quot;.

@Travers 
I&#039;m glad that you tried it. I&#039;m trying to explain how a Big code can&#039;t be a Bad Code ( as normally people think ).

Here are my results when I tried while I was writing this post.

For IF Condition test
1 - Time: 37.5
2 - Time: 29.7
3 - Time: 31.2


For the Problem test
1 - Time: 42.2
2 - Time: 34.4
3 - Time: 32.8

Please find the code for above two tests at http://drop.io/mymindleaks. You will find two java files.

I strongly belive, Manual optimization is more effective than Compiler Optimization.</description>
		<content:encoded><![CDATA[<p>@Bjarne, that was a silly mistake. I&#8217;ve corrected it. Thanks for pointing it. I&#8217;ve moved the &#8220;else if&#8221; as &#8220;if&#8221;.</p>
<p>@Travers<br />
I&#8217;m glad that you tried it. I&#8217;m trying to explain how a Big code can&#8217;t be a Bad Code ( as normally people think ).</p>
<p>Here are my results when I tried while I was writing this post.</p>
<p>For IF Condition test<br />
1 &#8211; Time: 37.5<br />
2 &#8211; Time: 29.7<br />
3 &#8211; Time: 31.2</p>
<p>For the Problem test<br />
1 &#8211; Time: 42.2<br />
2 &#8211; Time: 34.4<br />
3 &#8211; Time: 32.8</p>
<p>Please find the code for above two tests at <a href="http://drop.io/mymindleaks.">http://drop.io/mymindleaks.</a> You will find two java files.</p>
<p>I strongly belive, Manual optimization is more effective than Compiler Optimization.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Travers</title>
		<link>http://www.mymindleaks.com/blog/archives/big-code-good-code.html/comment-page-1#comment-982</link>
		<dc:creator>Travers</dc:creator>
		<pubDate>Fri, 09 May 2008 11:12:54 +0000</pubDate>
		<guid isPermaLink="false">http://mymindleaks.com/?p=622#comment-982</guid>
		<description>I can&#039;t agree with your assessments.  I spent 3 years doing assembly level optimization in the video game industry and working with output from Microsoft Visual C++ and GNU gcc, and I find your arguments specious.

Using C# (VS 2008 Express), I made a quick benchmark for your first example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestShortCode
{
    class Program
    {
        delegate int TestMethod(int a, int b);

        static int Method1(int a, int b) {
            int x = a &lt; b ? a : b;
            return x;
        }

        static int Method2(int a, int b)
        {
            int x = b;
            if (a &lt; b)
                x = a;
            return x;
        }

        static void Profile(TestMethod code)
        {
            Random r = new Random();
            code(0, 0);
            DateTime startTime = DateTime.Now;
            for (int i = 0; i &lt; 100000000; i++)
            {
                int a = r.Next(100);
                int b = r.Next(100);
                code(a, b);
            }
            DateTime endTime = DateTime.Now;
            TimeSpan delta = endTime - startTime;
            Console.WriteLine(delta);
        }

        static void Main(string[] args)
        {
            Profile(Method1);
            Profile(Method2);
        }
    }
}

Running it a half-dozen times, I got consistently the same results:
00:00:09.9218750
00:00:09.9375000

I notice people tend to ASSUME they know what their compilers are doing, but modern compilers do some crazy-ass stuff even with optimization turned off.

Your first example is not a good one because it ignores two things: 1)  you assume the compiler somehow does a &quot;return&quot; when in fact even in debug output it would generate case B, and 2) the branch is the single most expensive operation (writes go straight to the cache and on most CPUs have 0-cost writes, at least for single store instructions).

In VM contexts, the issue gets even more fuddled where the ad-hoc compiler can use profiling of your code to figure out which branch is more likely and arrange for the most common case to be the fall-through.

On to your major point: that is true that writing &quot;bigger&quot; code can lead to time savings--the most famous example is loop unwinding.  But as Donald Knuth liked to say &quot;Premature optimization is the root of all evil&quot;.  One should profile your code and _only_ optimize the hot-spots.  My personal experience has given me the following strategy:

For slow code try these steps (in order):
1. Change the algorithm.  In 3-D world rendering (like for Halo), back-face culling is a HELL of a lot faster than a faster polygon renderer. :-)

2. I/O bound is I/O bound.  If your CPU monitor is less than half-full but your HD light is solid, you&#039;re definitely I/O bound.  Reduce I/O access by using caching or memory mapping (&lt;-- most often overlooked speed-up for file-based processing).  If you are connecting to a database, figure out where in the database pipeline is the slowdown.  Most of the time, it&#039;s the query.  Be generous with indexing on SQL databases.

3. Watch your memory access.  This is very CPU/System dependent, but generally CPU time is cheaper than memory accessing (at least for C/C++/Assembly programming).  It can _sometimes_ be faster to calculate a value than to use a look-up table.  There are exceptions, but one should look at the actual assembly generated before deciding this because compilers can play mind-games on you.  In interpreted languages (like Perl &amp; Python), look-up tables are faster than calculations.</description>
		<content:encoded><![CDATA[<p>I can&#8217;t agree with your assessments.  I spent 3 years doing assembly level optimization in the video game industry and working with output from Microsoft Visual C++ and GNU gcc, and I find your arguments specious.</p>
<p>Using C# (VS 2008 Express), I made a quick benchmark for your first example:</p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.Text;</p>
<p>namespace TestShortCode<br />
{<br />
    class Program<br />
    {<br />
        delegate int TestMethod(int a, int b);</p>
<p>        static int Method1(int a, int b) {<br />
            int x = a &lt; b ? a : b;<br />
            return x;<br />
        }</p>
<p>        static int Method2(int a, int b)<br />
        {<br />
            int x = b;<br />
            if (a &lt; b)<br />
                x = a;<br />
            return x;<br />
        }</p>
<p>        static void Profile(TestMethod code)<br />
        {<br />
            Random r = new Random();<br />
            code(0, 0);<br />
            DateTime startTime = DateTime.Now;<br />
            for (int i = 0; i &lt; 100000000; i++)<br />
            {<br />
                int a = r.Next(100);<br />
                int b = r.Next(100);<br />
                code(a, b);<br />
            }<br />
            DateTime endTime = DateTime.Now;<br />
            TimeSpan delta = endTime &#8211; startTime;<br />
            Console.WriteLine(delta);<br />
        }</p>
<p>        static void Main(string[] args)<br />
        {<br />
            Profile(Method1);<br />
            Profile(Method2);<br />
        }<br />
    }<br />
}</p>
<p>Running it a half-dozen times, I got consistently the same results:<br />
00:00:09.9218750<br />
00:00:09.9375000</p>
<p>I notice people tend to ASSUME they know what their compilers are doing, but modern compilers do some crazy-ass stuff even with optimization turned off.</p>
<p>Your first example is not a good one because it ignores two things: 1)  you assume the compiler somehow does a &#8220;return&#8221; when in fact even in debug output it would generate case B, and 2) the branch is the single most expensive operation (writes go straight to the cache and on most CPUs have 0-cost writes, at least for single store instructions).</p>
<p>In VM contexts, the issue gets even more fuddled where the ad-hoc compiler can use profiling of your code to figure out which branch is more likely and arrange for the most common case to be the fall-through.</p>
<p>On to your major point: that is true that writing &#8220;bigger&#8221; code can lead to time savings&#8211;the most famous example is loop unwinding.  But as Donald Knuth liked to say &#8220;Premature optimization is the root of all evil&#8221;.  One should profile your code and _only_ optimize the hot-spots.  My personal experience has given me the following strategy:</p>
<p>For slow code try these steps (in order):<br />
1. Change the algorithm.  In 3-D world rendering (like for Halo), back-face culling is a HELL of a lot faster than a faster polygon renderer. <img src='http://www.mymindleaks.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>2. I/O bound is I/O bound.  If your CPU monitor is less than half-full but your HD light is solid, you&#8217;re definitely I/O bound.  Reduce I/O access by using caching or memory mapping (&lt;&#8211; most often overlooked speed-up for file-based processing).  If you are connecting to a database, figure out where in the database pipeline is the slowdown.  Most of the time, it&#8217;s the query.  Be generous with indexing on SQL databases.</p>
<p>3. Watch your memory access.  This is very CPU/System dependent, but generally CPU time is cheaper than memory accessing (at least for C/C++/Assembly programming).  It can _sometimes_ be faster to calculate a value than to use a look-up table.  There are exceptions, but one should look at the actual assembly generated before deciding this because compilers can play mind-games on you.  In interpreted languages (like Perl &amp; Python), look-up tables are faster than calculations.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
