<?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>So Jake Says &#187; C++</title>
	<atom:link href="http://www.jakevoytko.com/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jakevoytko.com/blog</link>
	<description>Ye Olde Computer Science Blogge</description>
	<lastBuildDate>Sun, 17 Jan 2010 15:16:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>A Code Workaround for a Busted Furnace</title>
		<link>http://www.jakevoytko.com/blog/2010/01/17/a-code-workaround-for-a-busted-furnace/</link>
		<comments>http://www.jakevoytko.com/blog/2010/01/17/a-code-workaround-for-a-busted-furnace/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 15:16:00 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[furnace]]></category>
		<category><![CDATA[threading]]></category>
		<category><![CDATA[workaround]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=406</guid>
		<description><![CDATA[warmup.cpp: [code] Late last December, I woke up and it was freezing in my apartment. I checked the thermostat, and it was already less than 60 degrees! I turned it from hot to cold to hot. And again. I turned it all the way up. The furnace never clicked on. COME ON! I checked, and [...]]]></description>
			<content:encoded><![CDATA[<p>warmup.cpp: [<a href="http://github.com/jakevoytko/warmup">code</a>]</p>
<p>Late last December, I woke up and it was <strong>freezing</strong> in my apartment. I checked the thermostat, and it was already less than 60 degrees! I turned it from hot to cold to hot. And again. I turned it all the way up. The furnace never clicked on. COME ON! I checked, and the pilot light was on, so I had no choice but to call maintenance to get it fixed.</p>
<p>Most days this wouldn&#8217;t have been a problem, since it would be fixed while I was at work, but I was in my end-of-year vacation-burning crunch. Maintenance always fix minor problems really fast, so I knew it would take all day to come and fix the heater. Sure enough, help didn&#8217;t arrive until 6PM.</p>
<p>I tried lying motionless on the floor under some blankets and played some Assassin&#8217;s Creed 2. This worked for an hour or so, but if I&#8217;m at home all day and not working on something on the computer, I start to feel like a waste. This was in the middle of trying to get <a href="http://www.vocabdojo.com/">a side project</a> of mine off the ground, and I wanted to get a beta version working before returning to work in January.</p>
<p>Could I use my laptop as a heater? It gets pretty warm when the CPU runs at full blast on one core. If I kept both going for hours, would that be enough?</p>
<p>8 minutes later, I had a C++ program that ran 2 CPUs at full blast. I hate picking C++ for a personal project, but I&#8217;ve used it for 10 years and I&#8217;m just not familiar enough with any other language&#8217;s CPU usage patterns to do the work that quickly. I probably didn&#8217;t have to worry, but I was cold!</p>
<p>The program, <code>warmup</code>, runs NUM_CPU copies of this thread:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> fn<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">bool</span> <span style="color: #000040;">*</span>go<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">double</span> a <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">static</span> mt19937 rng<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">static_cast</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000ff;">unsigned</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #0000dd;">time</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  normal_distribution<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000ff;">double</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> norm_dist<span style="color: #008000;">&#40;</span><span style="color:#800080;">1.0</span>, <span style="color:#800080;">1.0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  variate_generator<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span>mt19937<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>, normal_distribution<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000ff;">double</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>
  normal_sampler<span style="color: #008000;">&#40;</span>rng, norm_dist<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>go<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">double</span> val <span style="color: #000080;">=</span> normal_sampler<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    a <span style="color: #000040;">+</span><span style="color: #000080;">=</span> val<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I already had Boost, and I&#8217;d never used their pRNGs before, so I decided if it was a copy/paste job, I would give them a test run. Sure enough, it plugged right in.</p>
<p>The main function is even simpler:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">bool</span> go <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
  boost<span style="color: #008080;">::</span><span style="color: #007788;">scoped_ptr</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span>boost<span style="color: #008080;">::</span><span style="color: #007788;">thread</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> threads<span style="color: #008000;">&#91;</span>NUM_THREADS<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span>NUM_THREADS<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
  threads<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">reset</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">new</span> boost<span style="color: #008080;">::</span><span style="color: #007788;">thread</span><span style="color: #008000;">&#40;</span>fn, <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>go<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> asdf<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #FF0000;">&quot;enter any input and hit &amp;lt;Enter&amp;gt; to kill&quot;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span>endl<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cin</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>asdf<span style="color: #008080;">;</span>
&nbsp;
  go <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span>NUM_THREADS<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
    threads<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>join<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Within 12 minutes of starting, I was nice and toasty!</p>
<p>I posted the code on Github in case I needed it somewhere else: [<a href="http://github.com/jakevoytko/warmup">code</a>]<br />
﻿</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=406&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2010/01/17/a-code-workaround-for-a-busted-furnace/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Taking Advantage of (Read: Working Around) Syntax</title>
		<link>http://www.jakevoytko.com/blog/2008/11/03/taking-advantage-of-read-working-around-syntax/</link>
		<comments>http://www.jakevoytko.com/blog/2008/11/03/taking-advantage-of-read-working-around-syntax/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 04:00:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Abelman]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[C++ assignment]]></category>
		<category><![CDATA[C++ errors]]></category>
		<category><![CDATA[C++ workaround]]></category>
		<category><![CDATA[Sussman]]></category>
		<category><![CDATA[TODO]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=185</guid>
		<description><![CDATA[Syntax Smells &#8220;Programs must be written for people to read, and only incidentally for machines to execute.&#8221; ~Abelson and Sussman, SICP If 8 years of programming has taught me anything, it&#8217;s that programmers need to give themselves every advantage imaginable. I&#8217;m not disparaging other programmers.  I say this as someone who has been humbled on [...]]]></description>
			<content:encoded><![CDATA[<h2>Syntax Smells</h2>
<blockquote><p>&#8220;Programs must be written for people to read, and only incidentally for machines to execute.&#8221;</p>
<p>~Abelson and Sussman, SICP</p></blockquote>
<p>If 8 years of programming has taught me anything, it&#8217;s that programmers need to give themselves every advantage imaginable. I&#8217;m not disparaging other programmers.  I say this as someone who has been humbled on the altar of bugs many times.</p>
<p>The fact that my first language (C++) is one of the most complex languages probably didn&#8217;t help. C++ isn&#8217;t my favorite language, but I always find myself coming home to it, whether for school assignments, or more recently for money. Plus, it ranks higher than Java in my book. Yuck!</p>
<p>Most programming languages have fundamental shortcomings that prevent them from being very human-friendly. Like what? Like nested parentheses and braces and brackets. Fortunately, along with programming languages evolved code editors that do great things with indentation, coloring, and matching brace highlighting. The actual problem isn&#8217;t fixed (a colored, indented C++ program is still the exact same thing as a non-colored, non-indented C++ program), but the tools exist to make it a non-issue.</p>
<p><strong>There is a wrong way to do it, and a right way to do it, but none of it is enforced by the compiler.</strong></p>
<h2>Assignment Within if(){</h2>
<p>When I was 14 and 15 and hadn&#8217;t been using the language for very long, I made a whole litany of embarrassing mistakes. For instance, I would spend an hour trying to figure out why the following wasn&#8217;t doing what I expected:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #008000;">&#123;</span>
     do_something<span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Fortunately, I stopped falling for it after a few times. However, I couldn&#8217;t seem to shake the following error:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>my_ptr <span style="color: #000080;">=</span> <span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span> <span style="color: #666666;">// Wrong, but no compile error!</span>
     do_something<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>To the rookie, this looks correct at a quick glance. Even worse, it compiles without warning. In fact, this was one of the evil no-nos that Java outlawed altogether, even though it has legitimate uses.</p>
<p>No matter how many times I swore I&#8217;d look closer, 2 weeks later I&#8217;d find this same bug after 30 minutes of frustrated debugging.</p>
<p>Eventually, I eliminated a large subset of this error by writing the equality test differently:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span> <span style="color: #000080;">=</span> my_ptr<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#123;</span> <span style="color: #666666;">// compile error!</span>
     do_something<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>This caused a comparatively small boost in my productivity. However, this helped, and it required no effort. <strong>Every little bit counts.</strong> Interestingly, this kicked me out of my groove: I started making this error a whole lot less frequently because I was thinking more about my equalities. Unless projects have strict style guides, I still code it this way.</p>
<p>For being a simple style change, it comes with a few benefits:</p>
<ol>
<li>Anybody can understand this code.</li>
<li>When you screw up, it doesn&#8217;t compile.</li>
</ol>
<h2><strong>//TODO:</strong> Lists</h2>
<p>Some code generators, like Visual Studio 2003, fill generated functions with comments:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">void</span> importantFoo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
 <span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// TODO: Implement this!</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>For the authors of the code generator, this suits its purpose: informing the programmer that they have some work to do while generating compilable code.</p>
<p>Believe it or not, some code generators improve upon this. For instance, I&#8217;ve seen some code generators taking an exceptional approach:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">void</span> importantFoo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #0000ff;">throw</span> std<span style="color: #008080;">::</span><span style="color: #007788;">runtime_error</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;TODO: Implement function importantFoo()!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>This comes with benefits: unless the programmer is a complete ninny, this code will be executed, and the nascent program will be prematurely snuffed from this Earth.</p>
<p>If this doesn&#8217;t cause the programmer to get off his lazy rear end and code, then what will?</p>
<p>The // TODO! statement is certainly not unique to code generators: it <a href="http://www.google.com/codesearch?q=%2F%2F+TODO&amp;hl=en&amp;btnG=Search+Code">seems to be pretty popular</a> amongst programmers, too. I am no exception them, too.</p>
<p>For a while, I took the comment approach to the TODO:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">void</span> my_foo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     <span style="color: #666666;">// TODO: Implement me! I am a yummy pizza!</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>I&#8217;ve always felt a little dirty writing these. Realistically, your brain&#8217;s stack can only hold 5-9 items, so I can see an argument for temporarily using them. However, all you&#8217;re doing is passing the buck to an older, wiser future version of yourself in a brave new world with flying cars and butler monkeys.</p>
<p>It&#8217;s obvious that if you had flying cars and butler monkeys, the LAST thing that you would want to do would be to implement a // TODO statement left by a barbaric past self.</p>
<p>It&#8217;s my opinion that if you HAVE to write one, you should resolve the issue ASAP. For instance, before you  compile or interpret or &#8220;(read)&#8221; or whatever your language uses.</p>
<p>But recently, I found an even better TODO method for compiled languages:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #0000ff;">void</span> my_foo<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
     TODO<span style="color: #008080;">:</span> Implement me<span style="color: #000040;">!</span> I am a yummy pizza<span style="color: #000040;">!</span>
 <span style="color: #008000;">&#125;</span></pre></div></div>

<p>In C++, this generates about a trillion compiler errors (more or less). Compiler errors, for whatever reason, put me on a mission to fix a problem, so this is is an easy and helpful motivator.</p>
<p>I call these &#8220;Quit your bitchin&#8217;&#8221; TODO lists: Quit your bitchin&#8217; and implement me! You have to do it sooner or later!</p>
<p>It is important to note that you should NEVER do this in Python. Python is expressive enough that this syntax will likely cause the International Space Station to fall to earth while buttering bread on the wrong side. &lt;/bad_joke&gt;</p>
<h2>Conclusion</h2>
<p>Modern programming languages are designed  with human readability in mind, but there isn&#8217;t a &#8220;perfect language&#8221; in this regard. Best practices develop around programming languages, and is imperative that newbies find out what these are ASAP, because they are only hurting themselves if they do things their own way.</p>
<p>In addition to using best practices, sometimes you can make things easier for yourself with little effort.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=185&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/11/03/taking-advantage-of-read-working-around-syntax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lisp is Changing My C++</title>
		<link>http://www.jakevoytko.com/blog/2008/10/06/lisp-is-changing-my-c/</link>
		<comments>http://www.jakevoytko.com/blog/2008/10/06/lisp-is-changing-my-c/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 04:00:59 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Generalization]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Paul Graham]]></category>
		<category><![CDATA[Peter Norvig]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/?p=167</guid>
		<description><![CDATA[I am a mild-mannered C#/C++ programmer by day, and I have been learning Lisp in my spare time. Comparatively speaking, I do not spend much time programming for personal projects (only an hour or two a day), but I noticed something awesome recently: Lisp is influencing the way I write C++ in a few different [...]]]></description>
			<content:encoded><![CDATA[<p>I am a mild-mannered C#/C++ programmer by day, and I have been learning Lisp in my spare time. Comparatively speaking, I do not spend much time programming for personal projects (only an hour or two a day), but I noticed something awesome recently:</p>
<p>Lisp is influencing the way I write C++ in a few different ways.</p>
<h2>To Generalize&#8230;</h2>
<p>Imagine my horror: Lisp caused me to realize that I&#8217;ve been refactoring the <em>wrong way</em>! For a few years!</p>
<p>Well, that is not <em>entirely</em> accurate. My code did the same thing after I was done. I broke code down into small logical units. Unless I was careless, the code still worked. My functions were more readable, and one could usually see what they did at a glance.</p>
<p>I asked myself questions along the lines of, &#8220;What can I do to break this up logically?&#8221;, and therein lies the problem:</p>
<blockquote><p><strong>Weak questions have weak answers.</strong></p></blockquote>
<p>Powerful questions are much better suited to refactoring!</p>
<ul>
<li>&#8220;Can I use an existing solution to solve this?&#8221;</li>
<li>&#8220;Could this solve a completely unrelated problem?&#8221;</li>
<li>&#8220;Can I rearrange things elsewhere so that this code isn&#8217;t even needed?&#8221;</li>
</ul>
<p>Sometimes, the answer is no. Some boilerplate code (like exception catching) is an overhead that can not be avoided, can not usually be generalized, and sticks out like a sore thumb in C++ style languages.</p>
<p>Why did Lisp fundamentally shape my refactoring practices? Paul Graham and Peter Norvig are both to blame for this, and for different reasons.</p>
<p>First up, Graham. I taught myself from &#8220;<a href="http://www.jakevoytko.com/blog/2008/07/14/review-of-ansi-common-lisp/">ANSI Common Lisp</a>&#8220;. At first, I attacked the problems in the book from a C++ perspective, trying to wedge loops and functions into my solutions. Doing the Lisp examples forced me to think simpler. Within a month I started being able to think of functional solutions instead of C++ solutions (reaching for reduce and lambdas instead of loops and classes).</p>
<p>I worked problems out of this book every night for a month and a half, and it still sits next to me as a quick reference. After 8 years of C++ programming, it took a while for things like &#8220;reduce&#8221; to become the most obvious solution to a problem.</p>
<p>I haven&#8217;t reached that magical enlightening moment that Lisp causes. I don&#8217;t have the &#8220;ooh, shiny!&#8221; awe over macros that seasoned Lisp programmers seem to have, so I&#8217;m still hacking away.</p>
<p>Norvig has sent me towards enlightenment with <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2FParadigms-Artificial-Intelligence-Programming-Studies%2Fdp%2F1558601910&amp;tag=jakvoyshom-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325">Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=jakvoyshom-20&amp;l=ur2&amp;o=1" border="0" alt="" width="1" height="1" />. In these pages, I found something that was missing from Graham: elegance</p>
<p>Some of the solutions that I see in this book give me a sense of awe and enlightenment that I haven&#8217;t had since I took Abstract Algebra. He presents them in a very straightforward and honest style that can only come with experience.</p>
<p>This book has given me a whole new level of quality to try to reach. I hope one day to be able to do this.</p>
<h2>Tools</h2>
<p>A year ago, I would have described my programming process like this:</p>
<ol>
<li>Design</li>
<li>Code + document</li>
<li>Neaten + prune</li>
<li><em>Return to 1, as needed</em></li>
</ol>
<p>Not bad. However, working with Lisp and seeing that there are different tools in the world has led me to fundamentally restructure how I program. I would now describe my process like this:</p>
<ol>
<li>Design</li>
<li>Code + document</li>
<li>&#8220;Would a tool/function/class have made this task easier?&#8221;</li>
<li><em>Return to 1, as needed</em>.</li>
</ol>
<p>For instance, I&#8217;m writing some image processing libraries in CL right now, and I&#8217;ve written a few tools that I might never have thought of generalizing from a pure-C++ mindset! After all, iterating over an image is really easy in C++, so why bother making it simpler?</p>
<p>However, <code>map-image</code> is invaluable: apply a function at each pixel of an image, and return a new image. Combine this with the <code>lambda</code> construct, and you have a level of flexibility possible only with some of the goofier Boost libraries.</p>
<h2>In Short&#8230;</h2>
<p>While working through the Graham examples, I found that I was playing Code Golf against myself.</p>
<p>I&#8217;ve started to see this carry over to my coding at work as well. In the past, I might have done something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> num_times <span style="color: #000080;">=</span> SomeFunction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">return</span> AnotherFunction<span style="color: #008000;">&#40;</span>num_times<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>This is a reasonable approach. The local variable name reflects the role of the function, and adds a little bit of clarity for the human reader. Using this style is fine if these are your priorities.</p>
<p>However, I&#8217;ve started giving functions better names (when I can), and cutting out the middleman:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">return</span> AnotherFunction<span style="color: #008000;">&#40;</span>NumberOfTimes<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>This is effectively the same code, and there&#8217;s a good chance that it would compile down to the same assembly language. It&#8217;s just as readable. The difference is the line count. It&#8217;s a difference small enough for a <a href="http://www.bikeshed.com/">bikeshed issue holy war</a>, but over the course of small projects (all I&#8217;ve done so far in Lisp), being able to fit more code on my screen is a big win for me (all else equal).</p>
<p>I am starting to prefer reading and writing compressed code as a result of trying to solve Graham&#8217;s examples in as few lines as possible.</p>
<h2>Future Directions</h2>
<p>I&#8217;m still looking for my &#8220;Lisp Enlightenment&#8221; with respect to macros, which leads me to believe that there&#8217;s something significant that I&#8217;m still not getting about Common Lisp. I&#8217;m not sure where all of these new techniques are going to lead me, but in the meantime, it is starting to fundamentally change how I approach programming problems.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=167&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/10/06/lisp-is-changing-my-c/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Why Does RSA Work?</title>
		<link>http://www.jakevoytko.com/blog/2008/01/06/why-does-rsa-work/</link>
		<comments>http://www.jakevoytko.com/blog/2008/01/06/why-does-rsa-work/#comments</comments>
		<pubDate>Mon, 07 Jan 2008 02:05:54 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Decryption]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[Number Theory]]></category>
		<category><![CDATA[RSA]]></category>
		<category><![CDATA[Schneier]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2008/01/06/why-does-rsa-work/</guid>
		<description><![CDATA[To skip to the math, scroll down or click here. The Algorithm The algorithm is divided into three stages: precalculation, encryption, and decryption. Precalculation is performed a single time for each person with a public/private key pair, and encryption/decryption is performed for each message. Precalculation Pick primes: Find two arbitrarily large prime numbers, p and [...]]]></description>
			<content:encoded><![CDATA[<p><em>To skip to the math, scroll down or <a href="#rsa_math">click here.</a></em></p>
<p><img src="http://www.jakevoytko.com/blog/wp-content/uploads/2008/01/adleman_r_s.jpg" alt="adleman_r_s.jpg" /></p>
<h2>The Algorithm</h2>
<p>The algorithm is divided into three stages: precalculation, encryption, and decryption. Precalculation is performed a single time for each person with a public/private key pair, and encryption/decryption is performed for each message.</p>
<p><strong>Precalculation</strong></p>
<ol>
<li><span style="text-decoration: underline;">Pick primes</span><em>: </em>Find two arbitrarily large prime numbers, <em>p</em> and <em>q</em>.</li>
</ol>
<ol>
<li><span style="text-decoration: underline;">Determine the modulus</span>: Multiply <em>p </em>and <em>q</em> to get <em>n</em>. This will be your modulus for all equations.</li>
</ol>
<ol>
<li><span style="text-decoration: underline;">Calculate phi(n)</span>: Using Euler&#8217;s <a href="#totient">totient function</a>, calculate <img src='/blog/wp-content/plugins/latexrender/pictures/8c3daefe5bb907b8ba6db6b68be61371_3.5pt.gif' title='$\phi(n) = (p-1) * (q-1)$' alt='$\phi(n) = (p-1) * (q-1)$'  style="vertical-align:-3.5pt;" > . This number is a secret number, so don&#8217;t give it away. The strength of the algorithm depends on <img src='/blog/wp-content/plugins/latexrender/pictures/f4bdf2149704f6b9d6d0068d05021138_3.5pt.gif' title='$\phi(n)$' alt='$\phi(n)$'  style="vertical-align:-3.5pt;" > being hard to calculate when given a sufficiently large <em>n = p * q</em></li>
</ol>
<ol>
<li><span style="text-decoration: underline;">Determine an encryption exponent</span>: Take any number, <em>e</em>, such that GCD(<img src='/blog/wp-content/plugins/latexrender/pictures/f4bdf2149704f6b9d6d0068d05021138_3.5pt.gif' title='$\phi(n)$' alt='$\phi(n)$'  style="vertical-align:-3.5pt;" >, <em>e</em>) = 1. This means that they are <em><a href="http://www.jakevoytko.com/blog/2007/09/23/number-theory-for-programmers-part-2/">relatively prime</a></em>, and share no common factors. This number is considered your public key (when combined with <em>n</em>), and you can give this number to whoever you like.</li>
</ol>
<ol>
<li><span style="text-decoration: underline;">Compute the decryption exponent</span><em>:</em> Solve the <em><a href="#euclidean">Extended Euclidean Algorithm</a></em> of GCD(<img src='/blog/wp-content/plugins/latexrender/pictures/f4bdf2149704f6b9d6d0068d05021138_3.5pt.gif' title='$\phi(n)$' alt='$\phi(n)$'  style="vertical-align:-3.5pt;" >, e) to find <img src='/blog/wp-content/plugins/latexrender/pictures/506ad5480ebbbba8fc18288a504b6748_1.0pt.gif' title='e^{-1}' alt='e^{-1}'  style="vertical-align:-1.0pt;" >. This is your private key.</li>
</ol>
<p>We have a private decryption key pair: <em>{n, d}, </em>and a public encryption key pair: <em>{n, e}</em>.</p>
<p><strong>Encryption</strong></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/1aa9538eb07872740d6b24fb4b8d4f11_3.5pt.gif' title='$m_{1} \equiv m^{e} (mod\ n)$' alt='$m_{1} \equiv m^{e} (mod\ n)$'  style="vertical-align:-3.5pt;" ></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/96def595395241fe3de787a7597a9021_2.49998pt.gif' title='$m_{1}$' alt='$m_{1}$'  style="vertical-align:-2.49998pt;" > is the encrypted message, and it can safely be sent publicly.</p>
<p><strong>Decryption</strong></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/ca9c8e2a8355ae89777001437050f3f6_3.5pt.gif' title='$m_{2} \equiv m_{1}^d (mod\ n)$' alt='$m_{2} \equiv m_{1}^d (mod\ n)$'  style="vertical-align:-3.5pt;" ></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/8e3acbbfc26099340474ce98c66b8490_2.49998pt.gif' title='$m_{2}$' alt='$m_{2}$'  style="vertical-align:-2.49998pt;" > is the decrypted message. The person with the private key for the message will be able to read it, and theoretically, nobody else.</p>
<p><strong>Is the message preserved?</strong></p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/d867bc3f64c350ae7b5df118025ff437_3.5pt.gif' title='$m_{2} \equiv m_{1}^{d} (mod\ n) \equiv m^{ed} (mod\ n)' alt='$m_{2} \equiv m_{1}^{d} (mod\ n) \equiv m^{ed} (mod\ n)'  style="vertical-align:-3.5pt;" ></p>
<p>Applying the <em><a href="#euler_corollary">Corollary to Euler&#8217;s Theorem</a></em> to <img src='/blog/wp-content/plugins/latexrender/pictures/62139464e5724a75e09b2061874a68fc_3.5pt.gif' title='$m^{ed} (mod\ n)$' alt='$m^{ed} (mod\ n)$'  style="vertical-align:-3.5pt;" >, we get <img src='/blog/wp-content/plugins/latexrender/pictures/aa358c65fd3c4dca33ffbc8896a7510d_3.5pt.gif' title='$m^{1} (mod\ n) \equiv m$' alt='$m^{1} (mod\ n) \equiv m$'  style="vertical-align:-3.5pt;" >.</p>
<p>We also notice that <img src='/blog/wp-content/plugins/latexrender/pictures/b44b387c2fae8bb389fbef405fc6b930_3.5pt.gif' title='$m^{ed} \equiv m^{ee^{-1}} \equiv m (mod\ n)$' alt='$m^{ed} \equiv m^{ee^{-1}} \equiv m (mod\ n)$'  style="vertical-align:-3.5pt;" ></p>
<p><strong>Why is it hard for an attacker to crack?</strong></p>
<p>In a perfect world, the attacker needs to solve some equivalent of the <em><a href="http://en.wikipedia.org/wiki/Integer_factorization">Integer Factorization Problem</a></em> to factor <em>n</em>, which is suspected to be outside of complexity class P using classical computation. If we have access to quantum computers, we have access to an algorithm, <a href="http://en.wikipedia.org/wiki/Shor's_algorithm">Shor&#8217;s Algorithm</a>, to crack integer factorization efficiently, but probabilistically.</p>
<p>The essential problem that the attacker faces is as follows: they have the encryption exponent and the modulus. They know what <em>n</em> is, but they currently have no method of calculating what <img src='/blog/wp-content/plugins/latexrender/pictures/f4bdf2149704f6b9d6d0068d05021138_3.5pt.gif' title='$\phi(n)$' alt='$\phi(n)$'  style="vertical-align:-3.5pt;" > is without first factoring <em>n</em>. They need to find the decryption exponent, <em>d = <img src='/blog/wp-content/plugins/latexrender/pictures/947268967467ff4aa2ebe810f8c7b60f_1.0pt.gif' title='$e^{-1}$' alt='$e^{-1}$'  style="vertical-align:-1.0pt;" >,</em>, but can&#8217;t find the exponent without being able to solve the <em>Extended Euclidean Algorithm</em>, where they need to know the value of <img src='/blog/wp-content/plugins/latexrender/pictures/f4bdf2149704f6b9d6d0068d05021138_3.5pt.gif' title='$\phi(n)$' alt='$\phi(n)$'  style="vertical-align:-3.5pt;" >. So long as <em>n</em> is hard to factor, RSA will remain difficult to break.</p>
<p>We don&#8217;t live in a perfect world, and there are plenty of examples of attacks that take advantage of weak implementations of RSA. See <a href="http://tools.ietf.org/html/rfc3447#page-6">RFC 3447</a> for some best practices.</p>
<h2>The Math</h2>
<p><a name="rsa_math"></a><br />
<strong><a name="euclidean"></a>Extended Euclidean Algorithm</strong></p>
<p>The <em>Euclidean Algorithm</em> finds the Greatest Common Divisor (GCD) of two integers, <em>a</em> and <em>b.</em> The <em>Extended Euclidean Algorithm</em> finds integers <em>m</em> and <em>n</em> in the following equation:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/e75dfd8e4bfee86535502787f332ac0d_3.5pt.gif' title='$GCD(a,\ b) = m * a + n * b' alt='$GCD(a,\ b) = m * a + n * b'  style="vertical-align:-3.5pt;" ></p>
<p><span style="text-decoration: underline;">Euclidean Algorithm</span></p>
<p>To see how it does this, we will look at an example. Let <em>a</em> = 200, <em>b</em> = 37. Please note that 200 = <img src='/blog/wp-content/plugins/latexrender/pictures/1ed346930917426bc46d41e22cc525ec_2.94444pt.gif' title='\phi' alt='\phi'  style="vertical-align:-2.94444pt;" >(101 * 3). Since (200, 37) = 1, this is the equivalent of trying to find 37-1 (mod 200). For previous writings on the material, click here.</p>
<blockquote>
<ol>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/f364ad298499010b6c41859eb464a297_1.83333pt.gif' title='$200 = 37 * 5 + 15$' alt='$200 = 37 * 5 + 15$'  style="vertical-align:-1.83333pt;" ></li>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/5e4e4077ef4f088cbd9dc5aeb14f0728_1.83333pt.gif' title='$37 = 15 * 2 + 7$' alt='$37 = 15 * 2 + 7$'  style="vertical-align:-1.83333pt;" ></li>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/98683372abe86293f38b37105d32c221_1.83333pt.gif' title='$15 = 7 * 2 + 1$' alt='$15 = 7 * 2 + 1$'  style="vertical-align:-1.83333pt;" ></li>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/c6c639e36962dc17e3ae1299e298462c_1.83333pt.gif' title='$7 = 1 * 7 + 0$' alt='$7 = 1 * 7 + 0$'  style="vertical-align:-1.83333pt;" ></li>
</ol>
<p>Line 3 is considered the &#8220;final&#8221; line of the algorithm because it is the last line where the remainder is nonzero. The remainder of that line, 1, is also GCD(200, 37).</p></blockquote>
<p><span style="text-decoration: underline;">Extended Euclidean Algorithm</span></p>
<blockquote><p>We start by rewriting every equation so that the remainder is on the RHS.</p>
<ol>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/6f1119aaa4676402b94a91b8c419a7d7.gif' title='$15 = 200 &amp;#8211; 5 * 37$' alt='$15 = 200 &amp;#8211; 5 * 37$'  align=absmiddle></li>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/a2116d8125db7b822e12a7061da88670.gif' title='$7 = 37 &amp;#8211; 2 * 15$' alt='$7 = 37 &amp;#8211; 2 * 15$'  align=absmiddle></li>
<li><img src='/blog/wp-content/plugins/latexrender/pictures/b8b9d4527b1cfbe334daa095ecc21765.gif' title='$1 = 15 &amp;#8211; 2 * 7$' alt='$1 = 15 &amp;#8211; 2 * 7$'  align=absmiddle></li>
</ol>
<p>From here, we start at line 3, and substitute in line 2. Don&#8217;t simplify any multiplications, just the additions.</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/56ae81fde4832cce6d1c14b9afdebafb.gif' title='$1 = 15 &amp;#8211; 2 * 7 = 15 &amp;#8211; 2 * (37 &amp;#8211; 2 * 15)$' alt='$1 = 15 &amp;#8211; 2 * 7 = 15 &amp;#8211; 2 * (37 &amp;#8211; 2 * 15)$'  align=absmiddle><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/4f2911dc78845705774c34854ac36620.gif' title='$1 = 5 * 15 &amp;#8211; 2 * 37$' alt='$1 = 5 * 15 &amp;#8211; 2 * 37$'  align=absmiddle></p>
<p>Substitute line 1 into the resulting equation.</p>
<p>[Unparseable or potentially dangerous latex formula. Error 5 : 533x369]</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/95a8e87ded93c7161168f4963dce3c7d.gif' title='$1 = 5 * 200 &amp;#8211; 27 * 37$' alt='$1 = 5 * 200 &amp;#8211; 27 * 37$'  align=absmiddle></p></blockquote>
<p>We now have everything that we need for the inverse of 37. It turns out that <img src='/blog/wp-content/plugins/latexrender/pictures/95a8e87ded93c7161168f4963dce3c7d.gif' title='$1 = 5 * 200 &amp;#8211; 27 * 37$' alt='$1 = 5 * 200 &amp;#8211; 27 * 37$'  align=absmiddle> is equivalent to saying:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/f0a630494ed21992307526d3803cd79a_3.5pt.gif' title='$-27 * 37 \equiv 173 * 37 \equiv 1 (mod\ 200)' alt='$-27 * 37 \equiv 173 * 37 \equiv 1 (mod\ 200)'  style="vertical-align:-3.5pt;" ></p>
<p>so 173 is the multiplicative inverse of 37 (mod 200). Neat, huh?</p>
<p><strong><a name="Fermat"></a>Fermat&#8217;s Little Theorem + Euler&#8217;s Theorem</strong></p>
<p><img src="http://www.jakevoytko.com/blog/wp-content/uploads/2008/01/pierre_de_fermat.png" alt="pierre_de_fermat.png" /><br />
In a letter written in 1640, Fermat (of Pythagorean fame) offhandedly mentioned that he had noticed and proved the following relation:</p>
<p><span style="text-decoration: underline;">Fermat&#8217;s Little Theorem</span></p>
<blockquote><p>For <em>a</em> any integer, and <em>p</em> prime,</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/eed0a89e26b8b8947f6dff7d05933039_3.5pt.gif' title='$a^{p-1} \equiv 1 (mod\ p)$' alt='$a^{p-1} \equiv 1 (mod\ p)$'  style="vertical-align:-3.5pt;" ></p></blockquote>
<p>As he is <a href="http://en.wikipedia.org/wiki/Fermat's_last_theorem">occasionally noted for doing</a>, he did not bother to write down the proof. Leibniz is said to have proved it, but didn&#8217;t get around to publishing it. He was an inventor of calculus, so we&#8217;ll cut him some slack.</p>
<p><img src="http://www.jakevoytko.com/blog/wp-content/uploads/2008/01/leonhard_euler.jpg" alt="leonhard_euler.jpg" /></p>
<p>Enter Leonhard Euler. Euler was 18 feet tall, shot laser beams from his eyes, and <a href="http://en.wikipedia.org/wiki/List_of_topics_named_after_Leonhard_Euler">proved Mathematics every second he was awake</a>. In 1736, he took 23 minutes off from a conquest of Mars to prove Fermat&#8217;s Little Theorem.</p>
<p>This wasn&#8217;t enough, though. He attempted to find a way to generalize it for any number, <em>n</em>, instead of just for primes, <em>p</em>, but couldn&#8217;t.  The problem tortured him for 24 years, when in 1760* he was finally able to produce his <a href="http://www.cut-the-knot.org/blue/Euler.shtml">proof</a>.</p>
<p>His proof makes use of a function he defines, phi(x).</p>
<p><a name="totient"></a><span style="text-decoration: underline;">Euler&#8217;s Totient Function</span></p>
<blockquote><p>For any positive integer, <em>n</em>, <img src='/blog/wp-content/plugins/latexrender/pictures/f4bdf2149704f6b9d6d0068d05021138_3.5pt.gif' title='$\phi(n)$' alt='$\phi(n)$'  style="vertical-align:-3.5pt;" > is equal to the number of positive integers where GCD(<em>a, n</em>) = 1, for <em>a</em> &lt; <em>n</em>.</p>
<p>Most germane to this discussion, for any prime, <em>p</em>, <img src='/blog/wp-content/plugins/latexrender/pictures/1440b24e3db52e2fcb1b6cd9f6cb4035_3.5pt.gif' title='$\phi(n)$ = p-1' alt='$\phi(n)$ = p-1'  style="vertical-align:-3.5pt;" >. This makes perfect sense, of course, as a prime is indivisible, and therefore all numbers less than <em>p</em> don&#8217;t share factors with <em>p</em>, or else <em>p</em> could be divided!</p></blockquote>
<p>The formula ends up being:</p>
<p><span style="text-decoration: underline;">Euler&#8217;s Theorem</span></p>
<blockquote><p>For any positive integers <em>a, n</em>:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/7c39c47c1950b82a4fb0723bd97f801d_3.5pt.gif' title='$a^{\phi(n)} \equiv 1 (mod\ n)$' alt='$a^{\phi(n)} \equiv 1 (mod\ n)$'  style="vertical-align:-3.5pt;" ></p></blockquote>
<p>* Wikipedia says 1736, <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2FElementary-Number-Theory-Kenneth-Rosen%2Fdp%2F0321237072%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1199671115%26sr%3D8-2&amp;tag=jakvoyshom-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325">my number theory book</a><img style="border:none !important; margin:0px !important;" src="http://www.assoc-amazon.com/e/ir?t=jakvoyshom-20&amp;l=ur2&amp;o=1" border="0" alt="" width="1" height="1" /> says 1760, tie goes to the book.</p>
<p><strong><a name="euler_corollary"></a>Corollary to Euler&#8217;s Theorem</strong></p>
<p>Because <img src='/blog/wp-content/plugins/latexrender/pictures/7c39c47c1950b82a4fb0723bd97f801d_3.5pt.gif' title='$a^{\phi(n)} \equiv 1 (mod\ n)$' alt='$a^{\phi(n)} \equiv 1 (mod\ n)$'  style="vertical-align:-3.5pt;" >, we can reduce the multiplication needed for any power of <em>a</em> &gt; <em>n</em>.</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/ff8bed3e1d7d25a9f3ff424b32df92d4_3.5pt.gif' title='$a^{b} \equiv a^{b (mod\ \phi(n))} (mod\ n)$' alt='$a^{b} \equiv a^{b (mod\ \phi(n))} (mod\ n)$'  style="vertical-align:-3.5pt;" ></p>
<p><em>Edit: added link to Wikipedia&#8217;s article on Integer Factorization, and a better explanation of why the attack is hard</em></p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=31&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/01/06/why-does-rsa-work/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fun With String Searching</title>
		<link>http://www.jakevoytko.com/blog/2007/12/11/fun-with-string-searching/</link>
		<comments>http://www.jakevoytko.com/blog/2007/12/11/fun-with-string-searching/#comments</comments>
		<pubDate>Wed, 12 Dec 2007 05:41:53 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Boyer-Moore]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[KMP]]></category>
		<category><![CDATA[Knuth]]></category>
		<category><![CDATA[Morris]]></category>
		<category><![CDATA[Pratt]]></category>
		<category><![CDATA[Rabin-Karp]]></category>
		<category><![CDATA[String Searching]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2007/12/11/fun-with-string-searching/</guid>
		<description><![CDATA[Source of the program used. Almost every major program that works with text performs string searches. There are some juicy algorithms in this area, each with their own tradeoffs. Choosing the wrong algorithm for the wrong task produces awful results, and we need to carefully weigh the consequences of each algorithm. I&#8217;m going to take [...]]]></description>
			<content:encoded><![CDATA[<p><em><a title="Zip file of source" href="http://www.jakevoytko.com/blog/wp-content/uploads/2007/12/stringsearch.zip">Source</a> of the program used.</em></p>
<p>Almost every major program that works with text performs string searches. There are some juicy algorithms in this area, each with their own tradeoffs. Choosing the wrong algorithm for the wrong task produces awful results, and we need to carefully weigh the consequences of each algorithm. I&#8217;m going to take a look at just three of them: brute force, Boyer-Moore, and Rabin-Karp.</p>
<p>If you want to cut straight to the chase, feel free to scroll to the bottom to see the results section.</p>
<p>Why these three? Each of these algorithms offers a completely different perspective on the idea of finding strings. There are other algorithms that perform specialized tasks (Knuth-Morris-Pratt for repetitive source strings, for example), but we&#8217;re not interested in these today.</p>
<h3>Brute Force: Simple, and (Sometimes) Slow</h3>
<p>As we see in the image below, the brute force algorithm tries to generate a match from every single starting position.</p>
<p>It should be obvious to the reader that the algorithm can&#8217;t possibly miss a match, as every single position is tried. If the pattern exists in the text, brute force search will find it.</p>
<p><em>Note:  Green represents a single character match, and Red represents a single character mismatch.</em><br />
<a title="Brute Force image" href="http://www.jakevoytko.com/blog/wp-content/uploads/2007/12/bruteforcestring.png"><img src="http://www.jakevoytko.com/blog/wp-content/uploads/2007/12/bruteforcestring.png" alt="Brute Force image" /></a></p>
<p>The string &#8220;This is some Test text&#8221; isn&#8217;t very tough on the algorithm, as there is only one mismatch before we find the correct position of the word &#8220;Text&#8221;. However, it is very easy to construct pathological cases for this algorithm. It doesn&#8217;t perform well for binary data, or any highly-repetitive pattern. If it has to match the pattern of a whole bunch of <em>A</em>s with a <em>B</em>, it could very easily perform poorly. If we consider finding <em>AAA..AAB</em> in the string <em>AAA&#8230;&#8230;&#8230;.AAAB</em> it&#8217;s obvious that the algorithm will be taken down a costly blind alley at every single position</p>
<p><strong>Implementation in C++</strong></p>
<p>To check to see if we have a match at any position, all we have to do is check to see if each character of the string is a match. If it is not, we can return <em>false</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">bool</span> brute_force_match<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span> text,
                       <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span> pattern, <span style="color: #0000ff;">int</span> pos<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// If the source text is shorter than the</span>
    <span style="color: #666666;">// pattern + the position, we can't possibly</span>
    <span style="color: #666666;">// have a match.</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>pos <span style="color: #000040;">+</span> pattern.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> text.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">int</span> pattern_size <span style="color: #000080;">=</span> pattern.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Check every single character for a mismatch.</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i</pre></div></div>

<p>To search every position, we use the following snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> brute_force<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span> text, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span> pattern<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> search_size <span style="color: #000080;">=</span> text.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> pattern.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000080;">=</span> search_size<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>brute_force_match<span style="color: #008000;">&#40;</span>text, pattern, i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">return</span> i<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Brutally simple!</p>
<p><strong>Complexity and Potential Problems</strong></p>
<p>In certain document types, brute force is a valid solution for finding a string. Looking for strings in natural language files is still somewhat fast, as the algorithm is likely to into mismatches pretty fast. There are better algorithms (like Boyer-Moore, as written below), but the implementation time of brute force is not to be taken lightly.</p>
<p>When we don&#8217;t have a source string with a large number of &#8220;false starts&#8221;, we can expect the runtime of the algorithm to be roughly linear with the size of the file. It will have a runtime of roughly <em>M+N</em>, for <em>M</em> = text size, and <em>N</em> = pattern size. However, for searching in binary strings, or in strings with many repetitions of close matches to our pattern, the runtime is closer to <em>M*N</em>.</p>
<p>It takes less than 1/10 of a second to find a small string at the end of &#8220;Moby Dick&#8221; (see below)! I wasn&#8217;t joking when I said that brute force is a fast algorithm for certain inputs. Treat this algorithm like Bubblesort: use it for the plainest and smallest inputs.</p>
<h3>Boyer-Moore: Right-to-Left Trickery</h3>
<p>One of the assumptions that we made for the brute force algorithm is the left-to-right search. The direction of the search isn&#8217;t a requirement. In fact, we can search through the text however we please!</p>
<p>Since we are given the whole pattern string, we have a tremendous amount of information at our disposal. We just need to know what to look for. For this algorithm, when we come across a mismatch, there might be information from the mismatch that helps us speed up the search.</p>
<p>Let&#8217;s see an example of how we can use right-to-left searching combined with mismatches to search faster:</p>
<p><em>Note:  Green represents a single character match, and Red represents a single character mismatch.</em><br />
<a title="Boyer-Moore img" href="http://www.jakevoytko.com/blog/wp-content/uploads/2007/12/boyermoorestring.png"><img src="http://www.jakevoytko.com/blog/wp-content/uploads/2007/12/boyermoorestring.png" alt="Boyer-Moore img" /></a></p>
<p>We see that on step 1, the &#8216;s&#8217; doesn&#8217;t match the &#8216;t&#8217;, so we slide the pattern forward 1. Please note that this will line the &#8216;s&#8217; in &#8220;Test&#8221; up with the &#8216;s&#8217; in &#8220;This&#8221;.</p>
<p>Step 2 demonstrates the magic of the algorithm: there isn&#8217;t a &lt;space&gt; at all in &#8220;Test&#8221;, so we can completely skip over the letter in the source text wholesale. We see this happen again in step 4 with the &#8216;o&#8217;. There is no &#8216;o&#8217; in &#8220;Test&#8221;, so why waste time trying to match it?</p>
<p>The Boyer-Moore algorithm takes advantage of these jumps. We search from left to right, looking for the first mismatch. When we find one, we can compute how far we&#8217;ll need to slide our search pattern ahead in order to match the character. We can also use a lookup table to speed up the calculation of how far we need to jump.</p>
<p><strong>Implementation in C++</strong></p>
<p>The size of the lookup table for a pattern of <em>N</em> characters is 256 * <em>N</em> bytes for ASCII alphabets, which is cake on modern computers. For larger alphabets, such as Unicode, you need an associative data container in order to be able to implement the algorithm in memory.</p>
<p>The algorithm itself is very easy to follow:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> boyer_moore<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span> text, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span> pattern<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">int</span> search_size<span style="color: #000080;">=</span> text.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> pattern.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">int</span> other_ind, word_start<span style="color: #008080;">;</span>
  vector <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> skip_table<span style="color: #008080;">;</span>
&nbsp;
  initialize_skip<span style="color: #008000;">&#40;</span>skip_table, pattern<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>word_start<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> word_start<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000080;">=</span>search_size<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>word_start<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span>pattern.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i<span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #000040;">--</span>i<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #666666;">// If there is a mismatch</span>
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>text<span style="color: #008000;">&#91;</span>word_start <span style="color: #000040;">+</span> i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> pattern<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        other_ind <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>text<span style="color: #008000;">&#91;</span>word_start <span style="color: #000040;">+</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #666666;">// Skip forward the proper number of steps.</span>
        <span style="color: #666666;">// Look this up in the lookup table.</span>
        word_start <span style="color: #000040;">+</span><span style="color: #000080;">=</span> skip_table<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>other_ind<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
        <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
&nbsp;
      <span style="color: #666666;">// If we get to the beginning of the string</span>
      <span style="color: #666666;">// and still haven't found a mismatch,</span>
      <span style="color: #666666;">// we have the position.</span>
      <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>i <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">return</span> word_start<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>As you see, once we&#8217;ve built the skip table, it&#8217;s very easy to look up how much the character pointer should skip forward at each step.</p>
<p>But how do we build the skip table? Let&#8217;s look at <code>initialize_skip()</code></p>
<p>Let&#8217;s say that we have the string &#8220;testing&#8221;. The skip table for the letter &#8216;i&#8217; will start with every single value initialized to the maximum skip, 5.</p>
<p>We then iterate backwards through the string, finding the first instance of each letter. If we find a &#8220;t&#8221; instead of an &#8220;i&#8221;, we can only skip 1. If we find &#8220;s&#8221; or &#8220;e&#8221;, we can skip 2 and 3 respectively.</p>
<p>Notice that we don&#8217;t assume that it might be the &#8220;t&#8221; that starts the word. If we did that, we might jump the pattern too far and miss the match. We only want the first match going from right to left.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> initialize_skip<span style="color: #008000;">&#40;</span>vector <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span> skip_table,
                    <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span> pattern<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> pattern_size <span style="color: #000080;">=</span> pattern.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i
 to_push<span style="color: #008000;">&#40;</span>ALPHABET_SIZE, max_skip<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Have we seen a character yet?</span>
    vector used<span style="color: #008000;">&#40;</span>ALPHABET_SIZE, <span style="color: #0000ff;">false</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">int</span> counter <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> j<span style="color: #000080;">=</span>i<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> j<span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span><span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #000040;">--</span>j<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #666666;">// If we haven't used a character</span>
      <span style="color: #666666;">// yet, add its value in the lookup table</span>
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">!</span>used<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>pattern<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        to_push<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>pattern<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><span style="color: #000080;">=</span>counter<span style="color: #008080;">;</span>
        used<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>pattern<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
&nbsp;
      counter<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    skip_table.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>to_push<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Complexity and Potential Problems</strong></p>
<p>The worst case runtime of this algorithm is still <em>N*M</em>, although the average case for many different kinds of text (including natural language), is roughly <em>M/N</em>.</p>
<p>Pathological cases are still easy to construct. Try coming up with one yourself, or scroll down to the test section to see a sample string that can be pathological!</p>
<p>Worst case aside, this algorithm is incredibly useful on many different kinds of inputs, <em>especially</em> natural text. Having an average of <em>M/N</em> runtime is awesome, and is heads and shoulders better than brute force.</p>
<h3>Rabin-Karp: Clever Use of Hash Values</h3>
<p><em><strong>Extreme Math Warning</strong></em></p>
<p>The above algorithms both suffer from the same weakness: It is easy to construct pathological cases. If consistency is the goal of your program, these algorithms obviously would fall flat on their face for certain input types. It doesn&#8217;t matter if the algorithm runs fast in some cases: as soon as your user has to wait for your program to respond, they&#8217;re not happy.</p>
<p>Enter the Rabin-Karp string searching method. It treats the string like it is a large number that has been entered into a hash table. We consider consecutive characters in a string all as the same number.</p>
<p>For example, have the string &#8220;abba&#8221;, with the alphabet size = 256, the hash value is calculated as follows:</p>
<p><img src='/blog/wp-content/plugins/latexrender/pictures/3e13f2edadcb426f4c474d93ce2d5d01_1.83333pt.gif' title='$a*256^{3} + b*256^{2} + b*256 + a$' alt='$a*256^{3} + b*256^{2} + b*256 + a$'  style="vertical-align:-1.83333pt;" ></p>
<p>For more on the subject, see my older posts on <a href="http://www.jakevoytko.com/blog/2007/09/16/number-theory-for-programmers-part-1/">number theory</a> and <a href="http://www.jakevoytko.com/blog/2007/09/30/number-theory-hash-tables-and-geometric-progressions/">hash tables</a>.</p>
<p>The algorithm is as follows: we first generate the hash of the pattern, and the hash of the first <em>N</em> characters of the text. If the two hash values are equal, great! If they&#8217;re not equal, we remove the highest term, multiply the value by the alphabet size, and then add the next term. Simple, right?</p>
<p>Actually, I didn&#8217;t even understand that. Let&#8217;s see it written out with TeX.</p>
<p>The source text string: <strong>aabba</strong><br />
The pattern to find: abba</p>
<p>The first four letters of the source, hashed:<br />
<img src='/blog/wp-content/plugins/latexrender/pictures/d39dfac15d8869018bde48727f775e81_3.5pt.gif' title='$a*256^{3} + a*256^{2} + b*256 + b (mod\ p)$' alt='$a*256^{3} + a*256^{2} + b*256 + b (mod\ p)$'  style="vertical-align:-3.5pt;" ></p>
<p>This is extraordinarily unlikely to produce a hash value that matches the hash of the text &#8220;abba&#8221;. We first remove the highest term, as we no longer want it in the hash.<br />
<img src='/blog/wp-content/plugins/latexrender/pictures/f38d74121339a949467463911d1b8dfb.gif' title='$a*256^{3} + a*256^{2} + b*256 + b &amp;#8211; a*256^{3}$' alt='$a*256^{3} + a*256^{2} + b*256 + b &amp;#8211; a*256^{3}$'  align=absmiddle><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/f6debaf04f6998f67d2060bf0930133f_3.5pt.gif' title='$\ =\ a*256^{2} + b*256 + b(mod\ p)$' alt='$\ =\ a*256^{2} + b*256 + b(mod\ p)$'  style="vertical-align:-3.5pt;" ></p>
<p>We then multiply the value by the alphabet size, 256:<br />
<img src='/blog/wp-content/plugins/latexrender/pictures/0b44e720378ca560f6c53aaa0bab33ca_3.5pt.gif' title='$256 * (a*256^{2} + b*256 + b)$' alt='$256 * (a*256^{2} + b*256 + b)$'  style="vertical-align:-3.5pt;" ><br />
<img src='/blog/wp-content/plugins/latexrender/pictures/32a9a9b8e11a4f8235b8edc2d6fb2103_3.5pt.gif' title='$\ =\ a*256^{3} + b*256^{2} + b*256(mod\ p)$' alt='$\ =\ a*256^{3} + b*256^{2} + b*256(mod\ p)$'  style="vertical-align:-3.5pt;" ></p>
<p>And add the next character in the sequence, a:<br />
<img src='/blog/wp-content/plugins/latexrender/pictures/153a9472031d88b785993e1453fd4471_3.5pt.gif' title='$a*256^{3} + b*256^{2} + b*256 + a(mod\ p)$' alt='$a*256^{3} + b*256^{2} + b*256 + a(mod\ p)$'  style="vertical-align:-3.5pt;" ></p>
<p>Which obviously DOES hash to the value of the search string &#8220;abba&#8221;, as it&#8217;s the exact same thing we had above. Brilliant!</p>
<p>There <em>is</em> a small chance that there will be a collision for text values that don&#8217;t match our source string. We still need to check the string in place in order to make sure that we have the correct string, and not one that coincidentally matches.</p>
<p><strong>Implementation in C++</strong></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> rabin_karp<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span> text, <span style="color: #0000ff;">const</span> string<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span> pattern<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> search_size<span style="color: #000080;">=</span> text.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> pattern.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> pattern_size <span style="color: #000080;">=</span> pattern.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// A large prime such that prime &amp;lt; (2^32)/(ALPHABET_SIZE+1)</span>
    <span style="color: #666666;">//</span>
    <span style="color: #666666;">// This is our hash value</span>
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> prime <span style="color: #000080;">=</span> <span style="color: #0000dd;">5800079</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> alphabetM<span style="color: #000080;">=</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Less likely to overflow than powmod()</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i
<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000080;">=</span>search_size<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>patternHash <span style="color: #000080;">==</span> textHash <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span> brute_force_match<span style="color: #008000;">&#40;</span>text, pattern, i<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #0000ff;">return</span> i<span style="color: #008080;">;</span>
&nbsp;
        textHash <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>textHash <span style="color: #000040;">+</span> ALPHABET_SIZE<span style="color: #000040;">*</span>prime
                        <span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>text<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">*</span>alphabetM<span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> prime<span style="color: #008080;">;</span>
&nbsp;
        textHash <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>textHash<span style="color: #000040;">*</span>ALPHABET_SIZE
                        <span style="color: #000040;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span>text<span style="color: #008000;">&#91;</span>i<span style="color: #000040;">+</span>pattern_size<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> prime<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Complexity and Potential Problems</strong></p>
<p>The worst case of this algorithm is <em>M*N</em>, if you were to come up with a document where every single location hashes to the same value. However, this is <em>extremely</em> unlikely to ever happen, even if you&#8217;re trying to craft the worst-case document. My test of &#8220;Moby Dick&#8221; didn&#8217;t even come up with a single collision in the entire text of the novel, and the book is pretty damn big.</p>
<h3>Running the Algorithms</h3>
<p>Let&#8217;s take a look at how the algorithms fare when they compete to find the string &#8220;devious-cruising Rachel&#8221; in the text of &#8220;Moby Dick&#8221;. Aside from being a hysterical phrase, &#8220;devious-cruising Rachel&#8221; appears a single time in the book: in the very last paragraph.</p>
<p><em>The program was compiled with -O2</em></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Loading Moby Dick
&nbsp;
Text length is <span style="color: #0000dd;">1201110</span> characters
&nbsp;
Position of <span style="color: #FF0000;">&quot;devious-cruising Rachel&quot;</span><span style="color: #008080;">:</span> <span style="color: #0000dd;">1201001</span>
Brute force took <span style="color:#800080;">0.007865</span> seconds
&nbsp;
Position of <span style="color: #FF0000;">&quot;devious-cruising Rachel&quot;</span><span style="color: #008080;">:</span> <span style="color: #0000dd;">1201001</span>
Boyer<span style="color: #000040;">-</span>Moore took <span style="color:#800080;">0.001762</span> seconds
&nbsp;
Position of <span style="color: #FF0000;">&quot;devious-cruising Rachel&quot;</span><span style="color: #008080;">:</span> <span style="color: #0000dd;">1201001</span>
Rabin<span style="color: #000040;">-</span>Karp took <span style="color:#800080;">0.025224</span> seconds</pre></div></div>

<p>And Boyer-Moore is the winner by a long shot!</p>
<p>Rabin-Karp is much slower on this run! However, we are going to get consistent performance on that algorithm, so don&#8217;t count it out yet.</p>
<p>Let&#8217;s see how the various methods deal with the worst-case for the brute force algorithm:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Loading Brute Force torture test
&nbsp;
Text length is <span style="color: #0000dd;">11015500</span> characters
&nbsp;
Position of <span style="color: #FF0000;">&quot;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab&quot;</span>
        <span style="color: #008080;">:</span> <span style="color: #0000dd;">110113</span>
Brute force took <span style="color:#800080;">0.010162</span> seconds
&nbsp;
Position of <span style="color: #FF0000;">&quot;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab&quot;</span>
        <span style="color: #008080;">:</span> <span style="color: #0000dd;">110113</span>
Boyer<span style="color: #000040;">-</span>Moore took <span style="color:#800080;">0.001673</span> seconds
&nbsp;
Position of <span style="color: #FF0000;">&quot;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab&quot;</span>
        <span style="color: #008080;">:</span> <span style="color: #0000dd;">110113</span>
Rabin<span style="color: #000040;">-</span>Karp took <span style="color:#800080;">0.00218</span> seconds</pre></div></div>

<p>Rabin-Karp has pulled into second for this particular run. Again, it&#8217;s a consistent bugger.</p>
<p>And now let&#8217;s look at a worst-case file for the Boyer-Moore algorithm:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">Loading Boyer<span style="color: #000040;">-</span>Moore torture test
&nbsp;
Text length is <span style="color: #0000dd;">11015556</span> characters
&nbsp;
Position of <span style="color: #FF0000;">&quot;baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&quot;</span>
        <span style="color: #008080;">:</span> <span style="color: #0000dd;">110154</span>
Brute force took <span style="color:#800080;">0.000656</span> seconds
&nbsp;
Position of <span style="color: #FF0000;">&quot;baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&quot;</span>
        <span style="color: #008080;">:</span> <span style="color: #0000dd;">110154</span>
Boyer<span style="color: #000040;">-</span>Moore took <span style="color:#800080;">0.010248</span> seconds
&nbsp;
Position of <span style="color: #FF0000;">&quot;baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&quot;</span>
        <span style="color: #008080;">:</span> <span style="color: #0000dd;">110154</span>
Rabin<span style="color: #000040;">-</span>Karp took <span style="color:#800080;">0.00221</span> seconds</pre></div></div>

<p>We see the Boyer-Moore algorithm has an awful runtime, from all of the backtracking that it is doing. Again, the Rabin-Karp algorithm is looking pretty consistent!</p>
<h3>Where Do I Go Next?</h3>
<p>The algorithms described above were written to find a single pattern in a string of text. We still haven&#8217;t considered the problem of finding all of the matches for a certain string in a document. There is also the  Knuth-Morris-Pratt algorithm, an algorithm that works well for strings that are highly repetitive in nature, and is an improvement upon brute-force otherwise.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=23&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2007/12/11/fun-with-string-searching/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Use a Better Algorithm and Beat Haskell Today!</title>
		<link>http://www.jakevoytko.com/blog/2007/11/29/use-a-better-algorithm-and-beat-haskell-today/</link>
		<comments>http://www.jakevoytko.com/blog/2007/11/29/use-a-better-algorithm-and-beat-haskell-today/#comments</comments>
		<pubDate>Thu, 29 Nov 2007 23:55:34 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Insolent]]></category>
		<category><![CDATA[MPZ]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2007/11/29/use-a-better-algorithm-and-beat-haskell-today/</guid>
		<description><![CDATA[Note: This refers to the article &#8220;Use those extra cores and beat C today! (Parallel Haskell redux)&#8221;, and should be viewed as a good-natured ribbing! I&#8217;ll see your sensational headline and raise you a better algorithm! The Code: #include &#34;gmpxx.h&#34; #include #include &#160; using namespace std; &#160; mpz_class fib&#40;unsigned int num&#41; &#123; mpz_class a&#40;1&#41;, b&#40;1&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p><em>Note: This refers to the article &#8220;<a href="http://www.cse.unsw.edu.au/%7Edons/blog/2007/11/29#smoking-4core">Use those extra cores and beat C today! (Parallel Haskell redux)&#8221;</a>, and should be viewed as a good-natured ribbing!</em></p>
<p>I&#8217;ll see your sensational headline and raise you a better algorithm!</p>
<p><strong>The Code:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &quot;gmpxx.h&quot;</span>
<span style="color: #339900;">#include </span>
<span style="color: #339900;">#include </span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
mpz_class fib<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> num<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  mpz_class a<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>, b<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  mpz_class temp<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span>num<span style="color: #000040;">--</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    temp <span style="color: #000080;">=</span> a<span style="color: #008080;">;</span>
    a <span style="color: #000080;">=</span> a <span style="color: #000040;">+</span> b<span style="color: #008080;">;</span>
    b <span style="color: #000080;">=</span> temp<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">return</span> a<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #339900;">#define NUM 200000</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000dd;">cout</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #FF0000;">&quot;Fibonacci test for Fib(&quot;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #FF0000;">&quot;)&quot;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #FF0000;">&quot;Time: &quot;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #FF0000;">&quot; seconds&quot;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span></pre></div></div>

<p><strong>Build With:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">g<span style="color: #339933;">++</span> fib.<span style="color: #202020;">cpp</span> <span style="color: #339933;">-</span>O2 <span style="color: #339933;">-</span>lgmp <span style="color: #339933;">-</span>lgmpxx</pre></div></div>

<p><strong>The Result: </strong>(On a Toshiba Laptop w/ Core 2 Duo @ 1.6GHz)</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">  Fibonacci test <span style="color: #b1b100;">for</span> Fib<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">200000</span><span style="color: #009900;">&#41;</span>
  Time<span style="color: #339933;">:</span> <span style="color:#800080;">2.44233</span> seconds</pre></div></div>

<p>That&#8217;s a lot better than what we can get in Haskell using the detailed multicore approach. 32 seconds to calculate fib(45)? Please!</p>
<p><strong>The Lesson:</strong></p>
<ol>
<li>There&#8217;s more than one way to get a speedup in your code, and you should carefully weigh your options when writing. This algorithm can be bettered if you have <a href="http://en.wikipedia.org/wiki/Fibonacci_number#Closed_form_expression">phi calculated to many digits</a>, or if you use a <a href="http://www.inwap.com/pdp10/hbaker/hakmem/recurrence.html#item14">better recurrence</a>, so I&#8217;m not claiming &#8220;optimal&#8221; results here!</li>
<li>Throwing more cores at a problem does not always solve it best</li>
<li>I found out that writing sensational headlines is actually pretty fun.</li>
</ol>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=22&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2007/11/29/use-a-better-algorithm-and-beat-haskell-today/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Basic Combinatorics for Programmers</title>
		<link>http://www.jakevoytko.com/blog/2007/10/29/basic-combinatorics-for-programmers/</link>
		<comments>http://www.jakevoytko.com/blog/2007/10/29/basic-combinatorics-for-programmers/#comments</comments>
		<pubDate>Mon, 29 Oct 2007 12:29:05 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Choice]]></category>
		<category><![CDATA[Combination]]></category>
		<category><![CDATA[Combinatorics]]></category>
		<category><![CDATA[Permutation]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2007/10/29/basic-combinatorics-for-programmers/</guid>
		<description><![CDATA[Man only likes to count his troubles, but he does not count his joys. ~Fyodor Dostoevsky What is Combinatorics? Combinatorics is the math behind counting. All problems that start with the phrase &#8220;How many ways..&#8221; are most likely combinatorics problem. Who is this page for? Programmers who have a simple background in Mathematics, or need [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>Man only likes to count his troubles, but he does not count his joys.<br />
~Fyodor Dostoevsky</em></p></blockquote>
<h3>What is Combinatorics?</h3>
<p>Combinatorics is the math behind counting. All problems that start with the phrase &#8220;How many ways..&#8221; are most likely combinatorics problem.</p>
<h3>Who is this page for?</h3>
<ul>
<li>Programmers who have a simple background in Mathematics, or need a quick reference for how to implement combinatorial algorithms.</li>
<li>Those who did not know what the word &#8220;combinatorics&#8221; meant.</li>
</ul>
<h3>Factorials</h3>
<p>Factorials are used to solve the problem, &#8220;How many ways can I arrange <em>n</em> objects?&#8221; It makes the following assumptions:</p>
<ul>
<li>Each object is distinguishable (Can I tell apart the <em>n</em> apples?)</li>
<li>You can actually distinguish arrangements of objects (Can I line up <em>n</em> apples in a meaningful way?)</li>
<li>The elements are non-replaceable (Once I use an apple, I can&#8217;t use an apple again).</li>
</ul>
<p>The factorial function is defined as follows for positive integers:<br />
Factorial(1) = 1<br />
Factorial(n) = n * Factorial(n-1)</p>
<p>or, the naieve C implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">  <span style="color: #666666;">// For 32 bit systems, will not return meaningful results</span>
  <span style="color: #666666;">// for values larger than 12!</span>
  <span style="color: #666666;">//</span>
  <span style="color: #666666;">// For 64-bit systems, will not return meaningful results</span>
  <span style="color: #666666;">// for values larger than 20!</span>
  <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> n<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> ret <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span> i<span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000080;">=</span>n<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
        ret<span style="color: #000040;">*</span><span style="color: #000080;">=</span>i<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> ret<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></div></div>

<p>The C implementation above is fine for the values it can logically compute. However, when we get into arbitrary precision integer calculations, calculating <em>1 * 2 * 3 * &#8230; * n</em> is inefficient! The number of words produced at the end of each step grows linearly. If we divide-and-conquer, we keep down the number of words-per-integer until the very last few multiplications, which produces significant time savings.</p>
<p>Of course, we would NEVER use this in the real world without extra sanity checking of the input. The importance of sanity checks in the real world is demonstrated every day by celebrities. Don&#8217;t commit their same blunders.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">  mpz_class factorial<span style="color: #008000;">&#40;</span>mpz_class begin, mpz_class end<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>end<span style="color: #000080;">==</span>begin<span style="color: #008000;">&#41;</span>
      <span style="color: #0000ff;">return</span> end<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>end<span style="color: #000040;">-</span>begin <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span>
      <span style="color: #0000ff;">return</span> begin<span style="color: #000040;">*</span>end<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">int</span> half <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>begin<span style="color: #000040;">+</span>end<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> factorial<span style="color: #008000;">&#40;</span>begin, half<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> factorial<span style="color: #008000;">&#40;</span>half<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span>, end<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></div></div>

<p>In fact, running the two against each other to find 100,000! produces the following runtimes on my laptop (I was going to do 1 million and got bored waiting for the iterative solution to finish!):</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">  g<span style="color: #000040;">++</span> factorial.<span style="color: #007788;">cpp</span> <span style="color: #000040;">-</span>O2 <span style="color: #000040;">-</span>lgmp <span style="color: #000040;">-</span>lgmpxx
  .<span style="color: #000040;">/</span>a.<span style="color: #007788;">out</span>
  Recursive factorial<span style="color: #008080;">:</span> <span style="color:#800080;">0.2795</span> seconds
  Iterative factorial<span style="color: #008080;">:</span> <span style="color:#800080;">3.01006</span> seconds</pre></div></div>

<p>The most efficient ways to implement factorial algorithms turns out to be from its prime factorization, but the above is far easier than implementing sieving methods to find primes! Maybe another day <img src='http://www.jakevoytko.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Permutations</h3>
<p>Permutations are how we solve the problem, &#8220;how many different ways can I arrange <em>k</em> objects from a list of <em>n</em> objects? To give an example, assuming the same number could not be used twice in a combination, your &#8220;combination&#8221; lock from high school was actually a permutation lock. You were given a single permutation that you could use in order to open it. But how many different possible permutations were there, given 45 as a max value and 3 numbers?</p>
<ul>
<li>The first number has no restrictions. You can choose from all 45 numbers</li>
<li>The second number is restricted: it can&#8217;t be the same as the first number. You now have 44 to choose from, giving you <em>45 * 44</em> different possible 2-permutations of 45</li>
<li>The third number is also restricted: You can only choose 43 numbers. <em>45 * 44 * 43 </em>is the number of 3-permutations of 45.</li>
</ul>
<p>It&#8217;s not hard to convince ourselves that the permutation is all of the numbers between and including 45 and 43 multiplied together. Symbolically, we are talking about all of the numbers between and including <em>n</em> and <em>(n &#8211; k + 1)</em>, inclusive.</p>
<p>The formula we all learned in school (which you can convince yourself is equivalent to the above) is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">  <span style="color: #666666;">// Picking and ordering k objects from n objects</span>
  mpz_class permutation<span style="color: #008000;">&#40;</span>mpz_class n, mpz_class k<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">return</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, n<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, k<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>Never do this!</strong> You have to recompute many of the values that you never use, and it is worthless and computationally wasteful to do so. Instead, you can use the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">  mpz_class permutation<span style="color: #008000;">&#40;</span>mpz_class n, mpz_class k<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>k <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
          <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
      <span style="color: #0000ff;">return</span> factorial<span style="color: #008000;">&#40;</span>n <span style="color: #000040;">-</span> k <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span>, n<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></div></div>

<p>It&#8217;s much simpler. It computes much less. Use it. It is calling your name. Use it.</p>
<h3>Combinations</h3>
<p>Permutations are very closely related to combinations. In fact, there is only a one word difference in the definition. Combinations are <em>unordered</em>, and permutations are <em>ordered.</em> We can derive the formula by what this implies.</p>
<p>Let&#8217;s take our combination lock example. We needed to get three numbers in a row, in order. But what happens if we only need to select the three numbers without concern to the order? We know from the factorial section that the number of ways to order 3 numbers is 3 factorial. So let&#8217;s say we have the permutations { (1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1) }. These are all the same combination, because they all pick the same exact elements! So there is clearly 1/6 the number of combinations as permutations. 6 is 3!, which is no coincidence. In general, the formula for combinations is:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">  mpz_class combination<span style="color: #008000;">&#40;</span>mpz_class n, mpz_class k<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>k <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
          <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
      <span style="color: #0000ff;">return</span> permutation<span style="color: #008000;">&#40;</span>n, k<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span>factorial<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, k<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span></pre></div></div>

<p>*Hits the Easy Button*</p>
<p><strong><em>Next week:</em></strong><em> More complicated number sequences such as the Binomial Coefficients and the Sterling Numbers</em></p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=11&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2007/10/29/basic-combinatorics-for-programmers/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Basic Speed Optimizations for the Evil</title>
		<link>http://www.jakevoytko.com/blog/2007/10/07/basic-speed-optimizations-for-the-evil/</link>
		<comments>http://www.jakevoytko.com/blog/2007/10/07/basic-speed-optimizations-for-the-evil/#comments</comments>
		<pubDate>Mon, 08 Oct 2007 03:12:40 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[gprof]]></category>
		<category><![CDATA[Optimizations]]></category>
		<category><![CDATA[Profiling]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2007/10/07/basic-speed-optimizations-for-the-evil/</guid>
		<description><![CDATA[We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. ~Donald Knuth You&#8217;ve designed and written your application. You&#8217;ve carefully analyzed the algorithm, and it can&#8217;t possibly have a lower algorithmic complexity and still do its job. However, your program is still taking 11 seconds [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.<br />
~Donald Knuth </em></p></blockquote>
<p>You&#8217;ve designed and written your application. You&#8217;ve carefully analyzed the algorithm, and it can&#8217;t possibly have a lower algorithmic complexity and still do its job. However, your program is still taking 11 seconds to iterate through all data, and that&#8217;s absolutely unacceptable. You may now begin to optimize your code.</p>
<p>At this point, inexperienced programmers fumble the ball here: they begin to try to figure out where the bottleneck is in their program. By themselves. By looking at their code.</p>
<h3>Always profile your code when you want to improve it.</h3>
<p>No exceptions. When optimizing for speed, you always need to know which 20% of the code is taking up 80% of the execution time. A profiler will easily (and always) tell you which functions are being called the most, and how much time your program spends executing them. Experience can only give you a guess as to what should be improved, but profilers <strong>always</strong> tell you where the code is spending its time being executed.</p>
<p>It&#8217;s not magic, it&#8217;s common sense.</p>
<h3>Understand the Tradeoff of Optimization</h3>
<ul>
<li><strong>Legibility: </strong>What may read as an easy three-liner may be needlessly taking up 40% of the processor time.</li>
<li><strong>Development effort: </strong>Significant rewrites may be necessary in order to gain the performance boost suggested by the profiler.</li>
<li><strong>Maintenance: </strong>Extra development time may needed to be spent to write test cases for all of the changes that you have incorporated into the program. These cases, along with the (potentially) less legible code need to be maintained.</li>
<li><strong>Memory vs. Speed: </strong>Frequently, in order to have a faster program, we need to use more memory. Make sure your new code isn&#8217;t consuming unacceptable levels of memory.</li>
<li><strong>Platform-specific optimizations</strong>: Some optimizations are only possible on one computing platform, and others work far better on one platform than they do another. If your goal is truly platform-independent code, you have the burden of showing that your optimizations work on all platforms, and not just the target platform.</li>
</ul>
<h3>How will we profile code?</h3>
<p>I will be using <em><a href="http://www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html">gprof</a></em> and C++ in order to produce examples, but the ideas presented below will be universally applicable between procedural and object-oriented languages. I&#8217;ll let those who are experts with functional languages to talk about functional languages, and in my experience, they will.</p>
<p>I am not teaching you how to use gprof (though it is rather easy). I am showing you how to do some of the optimizations I most frequently find myself doing, and the lessons to be learned from them.</p>
<h3>Beware of Hidden Complexity!</h3>
<p>Assume we are trying to optimize a small code snippet, as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">vector a<span style="color: #008000;">&#40;</span>vect_size<span style="color: #008000;">&#41;</span>,
  b<span style="color: #008000;">&#40;</span>vect_size<span style="color: #008000;">&#41;</span>,
  result<span style="color: #008000;">&#40;</span>vect_size<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/*
  Snip: Initialize data, perform initial calculations, etc.
*/</span>
&nbsp;
<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> a.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
        result<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> a<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000040;">*</span> b<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>And you might find that the output for gprof would be something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"> <span style="color: #000040;">%</span>   cumulative   self              self     total
 <span style="color: #0000dd;">time</span>   seconds   seconds    calls  us<span style="color: #000040;">/</span>call  us<span style="color: #000040;">/</span>call  name
<span style="color:#800080;">15.66</span>      <span style="color:#800080;">0.06</span>     <span style="color:#800080;">0.03</span>  <span style="color: #0000dd;">4000000</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span> __gnu_cxx<span style="color: #008080;">::</span>__normal_iterator <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">operator</span><span style="color: #000040;">+</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">const</span><span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color:#800080;">12.53</span>      <span style="color:#800080;">0.08</span>     <span style="color:#800080;">0.02</span>  <span style="color: #0000dd;">2000002</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span> __gnu_cxx<span style="color: #008080;">::</span>__normal_iterator <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">base</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color:#800080;">9.40</span>       <span style="color:#800080;">0.09</span>     <span style="color:#800080;">0.02</span>  <span style="color: #0000dd;">4000000</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span>  std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color:#800080;">6.27</span>       <span style="color:#800080;">0.10</span>     <span style="color:#800080;">0.01</span>  <span style="color: #0000dd;">8000000</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span> __gnu_cxx<span style="color: #008080;">::</span>__normal_iterator <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span>__normal_iterator<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span><span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span><span style="color: #008000;">&#41;</span>
<span style="color:#800080;">6.27</span>       <span style="color:#800080;">0.11</span>     <span style="color:#800080;">0.01</span>  <span style="color: #0000dd;">4000000</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span> __gnu_cxx<span style="color: #008080;">::</span>__normal_iterator <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">operator</span><span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color:#800080;">6.27</span>       <span style="color:#800080;">0.12</span>     <span style="color:#800080;">0.01</span>  <span style="color: #0000dd;">4000000</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span>  std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">operator</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span>
<span style="color:#800080;">6.27</span>       <span style="color:#800080;">0.13</span>     <span style="color:#800080;">0.01</span>  <span style="color: #0000dd;">1000001</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span>  std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color:#800080;">6.27</span>       <span style="color:#800080;">0.14</span>     <span style="color:#800080;">0.01</span>  <span style="color: #0000dd;">1000001</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span>  std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color:#800080;">6.27</span>       <span style="color:#800080;">0.15</span>     <span style="color:#800080;">0.01</span>  <span style="color: #0000dd;">1000001</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span>  std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
<span style="color:#800080;">6.27</span>       <span style="color:#800080;">0.16</span>     <span style="color:#800080;">0.01</span>        <span style="color: #0000dd;">1</span>    <span style="color:#800080;">10.03</span>   <span style="color:#800080;">160.41</span>  calculate<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span></pre></div></div>

<p>These charts can be hard to read, but they extremely telling. The type of tasks that are taking up our processor time are: iterator manipulation.</p>
<p><strong>But wait, we&#8217;re not using iterators!</strong></p>
<p>Yes we are. Our calls to vector::operator[] are apparently taking up a lot more time than we ever could have imagined, and are using iterators in order to accomplish the task. Instead of using the index operator, let&#8217;s write the program the STL way and see what happens (Java, C, etc..  coders, take this as a general lesson instead of a language specific lesson!).</p>
<h3>Maximize the Utility of Function/Method Calls</h3>
<p>So we now replace all of the loop code with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">vector<span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> a_iter <span style="color: #000080;">=</span> a.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
vector<span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> b_iter <span style="color: #000080;">=</span> b.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span>vector<span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> r_iter <span style="color: #000080;">=</span> result.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    r_iter <span style="color: #000040;">!</span><span style="color: #000080;">=</span> result.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #000040;">++</span>r_iter<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #000040;">*</span>r_iter<span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>a_iter<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>b_iter<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #000040;">++</span>a_iter<span style="color: #008080;">;</span>
   <span style="color: #000040;">++</span>b_iter<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and we get the following output from gprof:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color:#800080;">20.88</span>      <span style="color:#800080;">0.06</span>     <span style="color:#800080;">0.03</span>  <span style="color: #0000dd;">1000001</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span>  std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color:#800080;">16.70</span>      <span style="color:#800080;">0.08</span>     <span style="color:#800080;">0.02</span>  <span style="color: #0000dd;">1000001</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span>  <span style="color: #0000ff;">bool</span> __gnu_cxx<span style="color: #008080;">::</span><span style="color: #007788;">operator</span><span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span><span style="color: #008000;">&#40;</span>__gnu_cxx<span style="color: #008080;">::</span>__normal_iterator  <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #0000ff;">const</span><span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>, __gnu_cxx<span style="color: #008080;">::</span>__normal_iterator <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #0000ff;">const</span><span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span><span style="color: #008000;">&#41;</span>
 <span style="color:#800080;">8.35</span>      <span style="color:#800080;">0.09</span>     <span style="color:#800080;">0.01</span>  <span style="color: #0000dd;">3000004</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span> __gnu_cxx<span style="color: #008080;">::</span>__normal_iterator <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span>__normal_iterator<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #000040;">*</span> <span style="color: #0000ff;">const</span><span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span><span style="color: #008000;">&#41;</span>
 <span style="color:#800080;">8.35</span>      <span style="color:#800080;">0.10</span>     <span style="color:#800080;">0.01</span>  <span style="color: #0000dd;">2000000</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span> __gnu_cxx<span style="color: #008080;">::</span>__normal_iterator <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">operator</span><span style="color: #000040;">++</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
 <span style="color:#800080;">8.35</span>      <span style="color:#800080;">0.11</span>     <span style="color:#800080;">0.01</span>  <span style="color: #0000dd;">1000001</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span>  std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span>
 <span style="color:#800080;">8.35</span>      <span style="color:#800080;">0.12</span>     <span style="color:#800080;">0.01</span>  <span style="color: #0000dd;">1000000</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span>  std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">operator</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">long</span><span style="color: #008000;">&#41;</span>
 <span style="color:#800080;">4.18</span>      <span style="color:#800080;">0.12</span>     <span style="color:#800080;">0.01</span>  <span style="color: #0000dd;">2000004</span>     <span style="color:#800080;">0.00</span>     <span style="color:#800080;">0.00</span> __gnu_cxx<span style="color: #008080;">::</span>__normal_iterator <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span> <span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;::</span><span style="color: #007788;">base</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span></pre></div></div>

<p>For those who are too lazy to scroll across (myself included), the function taking up 20% of the CPU time is&#8230; <em>vector::end()</em>! What&#8217;s happening is that we&#8217;re constantly constructing and returning an iterator to the last element in <em>r_iter</em>, and it&#8217;s hurting us, time-wise. Since the end element never changes, it makes no sense to constantly recalculate it. We should store it for future generations of the loop to enjoy, much as one might put the most recent copy of <em>People Magazine</em> in a time capsule for their children to enjoy the tales of celebrity compound-name relationships.</p>
<p>So, we rewrite our loop as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">vector<span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> a_iter <span style="color: #000080;">=</span> a.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
vector<span style="color: #008080;">::</span><span style="color: #007788;">const_iterator</span> b_iter <span style="color: #000080;">=</span> b.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
vector<span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> r_iter <span style="color: #000080;">=</span> result.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
vector<span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> r_end <span style="color: #000080;">=</span> result.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">;</span> r_iter <span style="color: #000040;">!</span><span style="color: #000080;">=</span> r_end<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>r_iter<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #000040;">*</span>r_iter<span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>a_iter<span style="color: #008000;">&#41;</span> <span style="color: #000040;">*</span> <span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>b_iter<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   <span style="color: #000040;">++</span>a_iter<span style="color: #008080;">;</span>
   <span style="color: #000040;">++</span>b_iter<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>and we observe another speed increase.</p>
<p>How much of a speed increase? The test program that I wrote in order to get these outputs took 5.11 seconds to run unoptimized. After profiling, it runs in 3.70 seconds. Not bad for a day&#8217;s work!</p>
<p>This hints on a broader lesson: <strong>reduce the crap in your loops as much as possible</strong>. Move it all as far outside as possible, and calculate it as few times as you can.</p>
<h3>Be Nice to the Cache</h3>
<p>While profiling can give us extraordinary speed increases solely by using innocuous code, it is not perfect. There is a much better way to write the above code that allows it to perform the same calculation with fewer cache misses. This rewrite would never ever be noticed solely through gprof.</p>
<p><a href="http://lwn.net/SubscriberLink/252125/568f5f3b7d53bd46/">This article</a> is an excellent overview of how modern CPUs implement the cache, and it certainly taught this Jake new tricks. I recommend that you at least glance through it in order to truly understand how it works.</p>
<p>So how do we write the above loop to better use the cache?</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> calc_info
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">int</span> data1, data2, result<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #ff0000; font-style: italic;">/*
  snip
*/</span>
&nbsp;
vector data<span style="color: #008080;">;</span>
&nbsp;
vector<span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> d_iter <span style="color: #000080;">=</span> data.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
vector<span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> d_end <span style="color: #000080;">=</span> data.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
calc_info<span style="color: #000040;">*</span> a<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">;</span> d_iter <span style="color: #000040;">!</span><span style="color: #000080;">=</span> d_end<span style="color: #008080;">;</span> <span style="color: #000040;">++</span>d_iter<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   a <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>d_iter<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   a<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>result <span style="color: #000080;">=</span> a<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>data1 <span style="color: #000040;">+</span> a<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>data2<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The above code finishes in 2.31 seconds. Why does it finish so fast? In the previous version of the program, whenever it calculated one of the result residuals, it took a value from the first vector and added it to a value from the second vector. For sufficiently large vectors, the values being calculated wouldn&#8217;t be anywhere NEAR each other in the memory. They wouldn&#8217;t even be in the same galaxy. It might even need to pause and fetch data from the disk. That&#8217;s a HUGE speed killer. The same thing could happen when it stores the residual as a result. Cache misses galore!</p>
<p>The rewritten method puts all data that is being calculated next to each other. There is rarely the need for the cache to load new data, because all of the data was already transmitted to the cache, and is sitting in memory next to each other. The profiler never would have turned it up.</p>
<h3>So should we avoid profiling?</h3>
<p>No. We got some excellent speed increases without having to resort to unsafe arrays, and we did it in next to no time just by using a profiler!</p>
<h3>Is profiling a complete substitute for experience?</h3>
<p>No. We got a huge speed boost just by knowing how the cache works, and I&#8217;m 100% certain that I missed details that would let it perform in even less time.</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=18&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2007/10/07/basic-speed-optimizations-for-the-evil/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
