<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Coyote Tracks &#187; big o notation</title>
	<atom:link href="http://kai.mactane.org/blog/tag/big-o-notation/feed/" rel="self" type="application/rss+xml" />
	<link>http://kai.mactane.org/blog</link>
	<description>The prints of an Internet-enabled coyote.</description>
	<lastBuildDate>Fri, 10 Sep 2010 01:40:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Would Shlemiel the Painter Optimize Prematurely?</title>
		<link>http://kai.mactane.org/blog/2009/06/18/would-shlemiel-the-painter-optimize-prematurely/</link>
		<comments>http://kai.mactane.org/blog/2009/06/18/would-shlemiel-the-painter-optimize-prematurely/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 21:34:51 +0000</pubDate>
		<dc:creator>Kai MacTane</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[big o notation]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[diary]]></category>
		<category><![CDATA[my projects]]></category>

		<guid isPermaLink="false">http://kai.mactane.org/blog/?p=73</guid>
		<description><![CDATA[I don&#8217;t want to optimize this code prematurely. And &#8220;while you&#8217;re still writing it&#8221; is probably premature. On the other hand, totally ignoring algorithmic complexity is a sure route to a Shlemiel the Painter&#8217;s algorithm.
Do I really want to just write the whole thing, and then start profiling it to see where the hot spots [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t want to <a href="http://shreevatsa.wordpress.com/2008/05/16/premature-optimization-is-the-root-of-all-evil/">optimize this code prematurely</a>. And &#8220;while you&#8217;re still writing it&#8221; is probably premature. On the other hand, totally ignoring algorithmic complexity is a sure route to a <a href="http://www.joelonsoftware.com/articles/fog0000000319.html">Shlemiel the Painter&#8217;s algorithm</a>.</p>
<p>Do I really want to just write the whole thing, and then start profiling it to see where the hot spots are, and then possibly have to re-design the whole thing? That seems like the complete opposite of &#8220;work smarter, not harder&#8221;. Then again, it doesn&#8217;t matter if you write an O(<i>n</i><sup>2</sup>) or even O(<i>n</i>!) algorithm if <i>n</i> is always going to be small&#8230; and in this application, I expect low-to-middling <i>n</i> values.</p>
<p>Of course, even if <i>n</i> will always be small, and increasing CPU power is my friend&#8230; even if it performs fast enough to make users happy, I&#8217;ll still know there&#8217;s a problem down there in the details. That may be the deciding factor.</p>
]]></content:encoded>
			<wfw:commentRss>http://kai.mactane.org/blog/2009/06/18/would-shlemiel-the-painter-optimize-prematurely/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>On Complexity Versus Efficiency</title>
		<link>http://kai.mactane.org/blog/2009/05/02/on-complexity-versus-efficiency/</link>
		<comments>http://kai.mactane.org/blog/2009/05/02/on-complexity-versus-efficiency/#comments</comments>
		<pubDate>Sun, 03 May 2009 01:39:04 +0000</pubDate>
		<dc:creator>Kai MacTane</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[big o notation]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[teaching]]></category>

		<guid isPermaLink="false">http://kai.mactane.org/blog/2009/05/02/on-complexity-versus-efficiency/</guid>
		<description><![CDATA[I sometimes imagine how I would teach certain concepts, if I were put in charge of a class. (Not just in programming, either; many people who know me have said I&#8217;d make a great teacher; perhaps I&#8217;ve taken it to heart.) One of the concepts in programming that I feel has a particularly poor &#8220;ease-of-teaching [...]]]></description>
			<content:encoded><![CDATA[<p>I sometimes imagine how I would teach certain concepts, if I were put in charge of a class. (Not just in programming, either; many people who know me have said I&#8217;d make a great teacher; perhaps I&#8217;ve taken it to heart.) One of the concepts in programming that I feel has a particularly poor &#8220;ease-of-teaching versus importance-of-knowing&#8221; ratio is <em>algorithmic complexity</em>, best described using <a href="http://en.wikipedia.org/wiki/Big_O_notation">Big&nbsp;O Notation</a>.</p>
<p>I just came across a great example of one use of Big&nbsp;O Notation, in examining the trade-off between conciseness and efficiency in Perl functions to find the smallest member of an array. While looking around for a good <a href="http://www.merriampark.com/ldperl.htm">Levenshtein algorithm implementation</a>, I noticed this helper function:</p>
<pre># minimal element of a list
#
sub min
{
    my @list = @{$_[0]};
    my $min = $list[0];

    foreach my $i (@list)
    {
        $min = $i if ($i < $min);
    }

    return $min;
}</pre>
<p>And I thought, "Hmmm, that's a little odd. Why go through that entire loop, when you could just do:"</p>
<pre>sub min {
	my @list = @{$_[0]};
	return (sort @list)[0];
}</pre>
<p>Of course, hard on its heels came the thought: "Wait a second... how do those stack up in Big&nbsp;O?"</p>
<p>Without even bothering to look at the source code, I'll assume Perl's <code>sort()</code> is an implementation of <a href="http://en.wikipedia.org/wiki/Quicksort">Quicksort</a>, and will achieve O(n&nbsp;log&nbsp;n) performance on most sane data. But the <code>min()</code> function supplied at Merriam Park is O(n); for any situation where log n is greater than 1, the n&nbsp;log&nbsp;n function will take longer than the just-plain&nbsp;n algorithm.</p>
<p>This would make a <em>wonderful</em> "teachable moment": point out to the class that my function is shorter, and easier to read and understand, but the other one is <em>more efficient</em>. This is exactly the sort of thing that Big&nbsp;O Notation is useful for, and it's something that can't be easily expressed just by looking at the code itself.</p>
<p>(Not that I'd try to tell the class that one way or the other was <em>automatically</em> better. The real usefulness of this example is the way the psychological complexity and the computational efficiency are in opposition to each other. It makes a great springboard to talk about how we decide on what trade-offs to make in engineering.)</p>
]]></content:encoded>
			<wfw:commentRss>http://kai.mactane.org/blog/2009/05/02/on-complexity-versus-efficiency/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
