<?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>Code for Concinnity</title>
	<atom:link href="http://cfc.kizzx2.com/index.php/feed/" rel="self" type="application/rss+xml" />
	<link>http://cfc.kizzx2.com</link>
	<description></description>
	<lastBuildDate>Wed, 09 May 2012 00:37:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>WinDBG + SOS: Failed to load data access DLL, 0&#215;80004005</title>
		<link>http://cfc.kizzx2.com/index.php/windbg-sos-failed-to-load-data-access-dll-0x80004005/</link>
		<comments>http://cfc.kizzx2.com/index.php/windbg-sos-failed-to-load-data-access-dll-0x80004005/#comments</comments>
		<pubDate>Fri, 10 Feb 2012 13:18:44 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=348</guid>
		<description><![CDATA[Today I received a dump file from a client. I fired up WinDBG and met this dreaded error. If you stumble upon this post you have probably tried all the other Google solutions (such as updating to the latest WinDBG). Here are what I tried, and failed: 12345&#62; .cordll -ve -u -l CLR DLL status: [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fwindbg-sos-failed-to-load-data-access-dll-0x80004005%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fwindbg-sos-failed-to-load-data-access-dll-0x80004005%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>Today I received a dump file from a client. I fired up WinDBG and met this dreaded error. If you stumble upon this post you have probably tried all the other Google solutions (such as updating to the latest WinDBG).</p>

<p>Here are what I tried, and failed:</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&gt; .cordll -ve -u -l<br />
CLR DLL status: No load attempts<br />
<br />
&gt; .exepath+ C:\path\to\mscordacwks.dll<br />
(No use)</div></td></tr></tbody></table></div>

<p>Here is how I solved it:</p>

<ul>
<li>First, I run the <code>.cordll</code> command that everybody recommends</li>
</ul>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&gt; .cordll<br />
CLR DLL status: ERROR: Unable to load DLL mscordacwks_x86_x86_2.0.50727.3625.dll, Win32 error 0n87</div></td></tr></tbody></table></div>

<ul>
<li>The client was running Windows Server 2003 SP2. The <code>mscordacwks.dll</code> I have in my Windows 7 box was 2.0.50727.5448</li>
</ul>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &gt; !sym noisy<br />
&nbsp; &nbsp; &gt; !clrstack<br />
<br />
&nbsp; &nbsp; CLRDLL: c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscordacwks.dll:2.0.50727.5448 f:0<br />
&nbsp; &nbsp; doesn't match desired version 2.0.50727.3625 f:0<br />
&nbsp; &nbsp; SYMSRV: &nbsp;c:\symcache\mscordacwks_x86_x86_2.0.50727.3625.dll\4E154C985a9000\mscordacwks_x86_x86_2.0.50727.3625.dll not found<br />
&nbsp; &nbsp; SYMSRV: &nbsp;http://msdl.microsoft.com/download/symbols/mscordacwks_x86_x86_2.0.50727.3625.dll/4E154C985a9000/mscordacwks_x86_x86_2.0.50727.3625.dll not found<br />
&nbsp; &nbsp; SYMSRV: &nbsp;C:\SymCache\mscordacwks_x86_x86_2.0.50727.3625.dll\4E154C985a9000\mscordacwks_x86_x86_2.0.50727.3625.dll not found<br />
&nbsp; &nbsp; SYMSRV: &nbsp;http://msdl.microsoft.com/download/symbols/mscordacwks_x86_x86_2.0.50727.3625.dll/4E154C985a9000/mscordacwks_x86_x86_2.0.50727.3625.dll not found<br />
&nbsp; &nbsp; CLRDLL: Unable to find mscordacwks_x86_x86_2.0.50727.3625.dll by mscorwks search<br />
&nbsp; &nbsp; CLRDLL: Unable to find 'mscordacwks_x86_x86_2.0.50727.3625.dll' on the path</div></td></tr></tbody></table></div>

<ul>
<li><p>So, it looks like this DLL is actually <strong>missing</strong> on MS&#8217;s server.</p></li>
<li><p>In a desperate atttempt, I tried to <a href="http://google.com/search?q=2.0.50727.3625">search the file in Google</a></p></li>
<li><p>To my surprise, I found the file in <a href="http://support.microsoft.com/kb/2572073">a security update</a></p></li>
<li><p>I downloaded the x86 file, opened the file in a zip and scavenged the required DLLs from there.</p></li>
<li><p>I had to rename the DLL files. In particular, I needed to duplicate <code>mscordacwks.dll</code> to <code>mscordacwks_x86_x86_2.0.50727.3625.dll</code> to please WinDBG</p></li>
<li><p>Load SOS manually <code>.load C:\newly\downloaded\sos.dll</code></p></li>
<li><p><code>.sympath+ C:\newly\downloaded; .reload</code></p></li>
<li><p>(You may encounter an error if you have 2 SOS loaded at the same time. <code>.chain</code>, <code>.unload</code> should take care of stuff)</p></li>
<li><p>Volia! Now the game begins <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/windbg-sos-failed-to-load-data-access-dll-0x80004005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binding C++ classes to Lua: a step by step example for beginners</title>
		<link>http://cfc.kizzx2.com/index.php/binding-c-classes-to-lua-a-step-by-step-example-for-beginners/</link>
		<comments>http://cfc.kizzx2.com/index.php/binding-c-classes-to-lua-a-step-by-step-example-for-beginners/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 14:32:09 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=345</guid>
		<description><![CDATA[Well, I lied. This isn&#8217;t step by step but I believe this commented source code dump should be more illustrative than any tutorial I&#8217;ve read, here we go]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fbinding-c-classes-to-lua-a-step-by-step-example-for-beginners%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fbinding-c-classes-to-lua-a-step-by-step-example-for-beginners%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>Well, I lied. This isn&#8217;t step by step but I believe this commented source code dump should be more illustrative than any tutorial I&#8217;ve read, here we go <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><div id="gist-1594905" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">// fun.cpp</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="k">extern</span> <span class="s">&quot;C&quot;</span></div><div class='line' id='LC4'><span class="p">{</span></div><div class='line' id='LC5'><span class="cp">#include &lt;lua.h&gt;</span></div><div class='line' id='LC6'><span class="cp">#include &lt;lauxlib.h&gt;</span></div><div class='line' id='LC7'><span class="cp">#include &lt;lualib.h&gt;</span></div><div class='line' id='LC8'><span class="p">}</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="cp">#include &lt;iostream&gt;</span></div><div class='line' id='LC11'><span class="cp">#include &lt;sstream&gt;</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'><span class="k">class</span> <span class="nc">Foo</span></div><div class='line' id='LC14'><span class="p">{</span></div><div class='line' id='LC15'><span class="k">public</span><span class="o">:</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Foo</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&amp;</span> <span class="n">name</span><span class="p">)</span> <span class="o">:</span> <span class="n">name</span><span class="p">(</span><span class="n">name</span><span class="p">)</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Foo is born&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">Add</span><span class="p">(</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span> <span class="kt">int</span> <span class="n">b</span><span class="p">)</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">std</span><span class="o">::</span><span class="n">stringstream</span> <span class="n">ss</span><span class="p">;</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">ss</span> <span class="o">&lt;&lt;</span> <span class="n">name</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">a</span> <span class="o">&lt;&lt;</span> <span class="s">&quot; + &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">b</span> <span class="o">&lt;&lt;</span> <span class="s">&quot; = &quot;</span> <span class="o">&lt;&lt;</span> <span class="p">(</span><span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="p">);</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">ss</span><span class="p">.</span><span class="n">str</span><span class="p">();</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC27'><br/></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">~</span><span class="n">Foo</span><span class="p">()</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Foo is gone&quot;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC32'><br/></div><div class='line' id='LC33'><span class="k">private</span><span class="o">:</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">name</span><span class="p">;</span></div><div class='line' id='LC35'><span class="p">};</span></div><div class='line' id='LC36'><br/></div><div class='line' id='LC37'><span class="c1">// The general pattern to binding C++ class to Lua is to write a Lua </span></div><div class='line' id='LC38'><span class="c1">// thunk for every method for the class, so here we go:</span></div><div class='line' id='LC39'><br/></div><div class='line' id='LC40'><span class="kt">int</span> <span class="n">l_Foo_constructor</span><span class="p">(</span><span class="n">lua_State</span> <span class="o">*</span> <span class="n">l</span><span class="p">)</span></div><div class='line' id='LC41'><span class="p">{</span></div><div class='line' id='LC42'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">const</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">name</span> <span class="o">=</span> <span class="n">luaL_checkstring</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span></div><div class='line' id='LC43'><br/></div><div class='line' id='LC44'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// We could actually allocate Foo itself as a user data but </span></div><div class='line' id='LC45'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// since user data can be GC&#39;ed and we gain unity by using CRT&#39;s heap </span></div><div class='line' id='LC46'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// all along.</span></div><div class='line' id='LC47'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Foo</span> <span class="o">**</span> <span class="n">udata</span> <span class="o">=</span> <span class="p">(</span><span class="n">Foo</span> <span class="o">**</span><span class="p">)</span><span class="n">lua_newuserdata</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">Foo</span> <span class="o">*</span><span class="p">));</span></div><div class='line' id='LC48'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">*</span><span class="n">udata</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Foo</span><span class="p">(</span><span class="n">name</span><span class="p">);</span></div><div class='line' id='LC49'><br/></div><div class='line' id='LC50'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Usually, we&#39;ll just use &quot;Foo&quot; as the second parameter, but I </span></div><div class='line' id='LC51'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// say luaL_Foo here to distinguish the difference:</span></div><div class='line' id='LC52'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//</span></div><div class='line' id='LC53'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// This 2nd parameter here is an _internal label_ for luaL, it is </span></div><div class='line' id='LC54'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// _not_ exposed to Lua by default.</span></div><div class='line' id='LC55'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//</span></div><div class='line' id='LC56'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Effectively, this metatable is not accessible by Lua by default.</span></div><div class='line' id='LC57'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">luaL_getmetatable</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="s">&quot;luaL_Foo&quot;</span><span class="p">);</span></div><div class='line' id='LC58'><br/></div><div class='line' id='LC59'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// The Lua stack at this point looks like this:</span></div><div class='line' id='LC60'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     </span></div><div class='line' id='LC61'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     3| metatable &quot;luaL_foo&quot;   |-1</span></div><div class='line' id='LC62'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     2| userdata               |-2</span></div><div class='line' id='LC63'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     1| string parameter       |-3</span></div><div class='line' id='LC64'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//</span></div><div class='line' id='LC65'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// So the following line sets the metatable for the user data to the luaL_Foo </span></div><div class='line' id='LC66'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// metatable</span></div><div class='line' id='LC67'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//</span></div><div class='line' id='LC68'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// We must set the metatable here because Lua prohibits setting </span></div><div class='line' id='LC69'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// the metatable of a userdata in Lua. The only way to set a metatable </span></div><div class='line' id='LC70'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// of a userdata is to do it in C.</span></div><div class='line' id='LC71'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">lua_setmetatable</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">);</span></div><div class='line' id='LC72'><br/></div><div class='line' id='LC73'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// The Lua stack at this point looks like this:</span></div><div class='line' id='LC74'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     </span></div><div class='line' id='LC75'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     2| userdata               |-1</span></div><div class='line' id='LC76'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     1| string parameter       |-2</span></div><div class='line' id='LC77'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// </span></div><div class='line' id='LC78'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// We return 1 so Lua callsite will get the user data and </span></div><div class='line' id='LC79'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Lua will clean the stack after that.</span></div><div class='line' id='LC80'><br/></div><div class='line' id='LC81'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="mi">1</span><span class="p">;</span></div><div class='line' id='LC82'><span class="p">}</span></div><div class='line' id='LC83'><br/></div><div class='line' id='LC84'><span class="n">Foo</span> <span class="o">*</span> <span class="n">l_CheckFoo</span><span class="p">(</span><span class="n">lua_State</span> <span class="o">*</span> <span class="n">l</span><span class="p">,</span> <span class="kt">int</span> <span class="n">n</span><span class="p">)</span></div><div class='line' id='LC85'><span class="p">{</span></div><div class='line' id='LC86'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// This checks that the argument is a userdata </span></div><div class='line' id='LC87'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// with the metatable &quot;luaL_Foo&quot;</span></div><div class='line' id='LC88'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="o">*</span><span class="p">(</span><span class="n">Foo</span> <span class="o">**</span><span class="p">)</span><span class="n">luaL_checkudata</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="s">&quot;luaL_Foo&quot;</span><span class="p">);</span></div><div class='line' id='LC89'><span class="p">}</span></div><div class='line' id='LC90'><br/></div><div class='line' id='LC91'><span class="kt">int</span> <span class="n">l_Foo_add</span><span class="p">(</span><span class="n">lua_State</span> <span class="o">*</span> <span class="n">l</span><span class="p">)</span></div><div class='line' id='LC92'><span class="p">{</span></div><div class='line' id='LC93'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Foo</span> <span class="o">*</span> <span class="n">foo</span> <span class="o">=</span> <span class="n">l_CheckFoo</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span></div><div class='line' id='LC94'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">a</span> <span class="o">=</span> <span class="n">luaL_checknumber</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span></div><div class='line' id='LC95'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">b</span> <span class="o">=</span> <span class="n">luaL_checknumber</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="mi">3</span><span class="p">);</span></div><div class='line' id='LC96'><br/></div><div class='line' id='LC97'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">s</span> <span class="o">=</span> <span class="n">foo</span><span class="o">-&gt;</span><span class="n">Add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">);</span></div><div class='line' id='LC98'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">lua_pushstring</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">s</span><span class="p">.</span><span class="n">c_str</span><span class="p">());</span></div><div class='line' id='LC99'><br/></div><div class='line' id='LC100'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// The Lua stack at this point looks like this:</span></div><div class='line' id='LC101'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     </span></div><div class='line' id='LC102'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     4| result string          |-1</span></div><div class='line' id='LC103'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     3| metatable &quot;luaL_foo&quot;   |-2</span></div><div class='line' id='LC104'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     2| userdata               |-3</span></div><div class='line' id='LC105'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     1| string parameter       |-4</span></div><div class='line' id='LC106'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//</span></div><div class='line' id='LC107'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Return 1 to return the result string to Lua callsite.</span></div><div class='line' id='LC108'><br/></div><div class='line' id='LC109'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="mi">1</span><span class="p">;</span></div><div class='line' id='LC110'><span class="p">}</span></div><div class='line' id='LC111'><br/></div><div class='line' id='LC112'><span class="kt">int</span> <span class="n">l_Foo_destructor</span><span class="p">(</span><span class="n">lua_State</span> <span class="o">*</span> <span class="n">l</span><span class="p">)</span></div><div class='line' id='LC113'><span class="p">{</span></div><div class='line' id='LC114'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Foo</span> <span class="o">*</span> <span class="n">foo</span> <span class="o">=</span> <span class="n">l_CheckFoo</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span></div><div class='line' id='LC115'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">delete</span> <span class="n">foo</span><span class="p">;</span></div><div class='line' id='LC116'><br/></div><div class='line' id='LC117'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="mi">0</span><span class="p">;</span></div><div class='line' id='LC118'><span class="p">}</span></div><div class='line' id='LC119'><br/></div><div class='line' id='LC120'><span class="kt">void</span> <span class="n">RegisterFoo</span><span class="p">(</span><span class="n">lua_State</span> <span class="o">*</span> <span class="n">l</span><span class="p">)</span></div><div class='line' id='LC121'><span class="p">{</span></div><div class='line' id='LC122'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">luaL_Reg</span> <span class="n">sFooRegs</span><span class="p">[]</span> <span class="o">=</span></div><div class='line' id='LC123'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC124'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span> <span class="s">&quot;new&quot;</span><span class="p">,</span> <span class="n">l_Foo_constructor</span> <span class="p">},</span></div><div class='line' id='LC125'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span> <span class="s">&quot;add&quot;</span><span class="p">,</span> <span class="n">l_Foo_add</span> <span class="p">},</span></div><div class='line' id='LC126'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span> <span class="s">&quot;__gc&quot;</span><span class="p">,</span> <span class="n">l_Foo_destructor</span> <span class="p">},</span></div><div class='line' id='LC127'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span> <span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span> <span class="p">}</span></div><div class='line' id='LC128'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">};</span></div><div class='line' id='LC129'><br/></div><div class='line' id='LC130'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Create a luaL metatable. This metatable is not </span></div><div class='line' id='LC131'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// exposed to Lua. The &quot;luaL_Foo&quot; label is used by luaL</span></div><div class='line' id='LC132'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// internally to identity things.</span></div><div class='line' id='LC133'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">luaL_newmetatable</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="s">&quot;luaL_Foo&quot;</span><span class="p">);</span></div><div class='line' id='LC134'><br/></div><div class='line' id='LC135'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Register the C functions _into_ the metatable we just created.</span></div><div class='line' id='LC136'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">luaL_register</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="n">sFooRegs</span><span class="p">);</span></div><div class='line' id='LC137'><br/></div><div class='line' id='LC138'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// The Lua stack at this point looks like this:</span></div><div class='line' id='LC139'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     </span></div><div class='line' id='LC140'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     1| metatable &quot;luaL_Foo&quot;   |-1</span></div><div class='line' id='LC141'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">lua_pushvalue</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">);</span></div><div class='line' id='LC142'><br/></div><div class='line' id='LC143'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// The Lua stack at this point looks like this:</span></div><div class='line' id='LC144'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     </span></div><div class='line' id='LC145'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     2| metatable &quot;luaL_Foo&quot;   |-1</span></div><div class='line' id='LC146'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     1| metatable &quot;luaL_Foo&quot;   |-2</span></div><div class='line' id='LC147'><br/></div><div class='line' id='LC148'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Set the &quot;__index&quot; field of the metatable to point to itself</span></div><div class='line' id='LC149'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// This pops the stack</span></div><div class='line' id='LC150'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">lua_setfield</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="s">&quot;__index&quot;</span><span class="p">);</span></div><div class='line' id='LC151'><br/></div><div class='line' id='LC152'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// The Lua stack at this point looks like this:</span></div><div class='line' id='LC153'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     </span></div><div class='line' id='LC154'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     1| metatable &quot;luaL_Foo&quot;   |-1</span></div><div class='line' id='LC155'><br/></div><div class='line' id='LC156'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// The luaL_Foo metatable now has the following fields</span></div><div class='line' id='LC157'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     - __gc</span></div><div class='line' id='LC158'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     - __index</span></div><div class='line' id='LC159'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     - add</span></div><div class='line' id='LC160'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//     - new</span></div><div class='line' id='LC161'><br/></div><div class='line' id='LC162'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Now we use setglobal to officially expose the luaL_Foo metatable </span></div><div class='line' id='LC163'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// to Lua. And we use the name &quot;Foo&quot;.</span></div><div class='line' id='LC164'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">//</span></div><div class='line' id='LC165'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// This allows Lua scripts to _override_ the metatable of Foo.</span></div><div class='line' id='LC166'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// For high security code this may not be called for but </span></div><div class='line' id='LC167'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// we&#39;ll do this to get greater flexibility.</span></div><div class='line' id='LC168'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">lua_setglobal</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="s">&quot;Foo&quot;</span><span class="p">);</span></div><div class='line' id='LC169'><span class="p">}</span></div><div class='line' id='LC170'><br/></div><div class='line' id='LC171'><span class="kt">int</span> <span class="n">main</span><span class="p">()</span></div><div class='line' id='LC172'><span class="p">{</span></div><div class='line' id='LC173'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">lua_State</span> <span class="o">*</span> <span class="n">l</span> <span class="o">=</span> <span class="n">luaL_newstate</span><span class="p">();</span></div><div class='line' id='LC174'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">luaL_openlibs</span><span class="p">(</span><span class="n">l</span><span class="p">);</span></div><div class='line' id='LC175'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">RegisterFoo</span><span class="p">(</span><span class="n">l</span><span class="p">);</span></div><div class='line' id='LC176'><br/></div><div class='line' id='LC177'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">erred</span> <span class="o">=</span> <span class="n">luaL_dofile</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="s">&quot;fun.lua&quot;</span><span class="p">);</span></div><div class='line' id='LC178'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span><span class="p">(</span><span class="n">erred</span><span class="p">)</span></div><div class='line' id='LC179'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Lua error: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">luaL_checkstring</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span></div><div class='line' id='LC180'><br/></div><div class='line' id='LC181'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">lua_close</span><span class="p">(</span><span class="n">l</span><span class="p">);</span></div><div class='line' id='LC182'><br/></div><div class='line' id='LC183'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="mi">0</span><span class="p">;</span></div><div class='line' id='LC184'><span class="p">}</span></div><div class='line' id='LC185'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1594905/3c3aaf5c7f1cd830969325010db4caec6860ba40/fun.cpp" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1594905#file_fun.cpp" style="float:right;margin-right:10px;color:#666">fun.cpp</a>
            <a href="https://gist.github.com/1594905">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<div id="gist-1594905" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">-- fun.lua</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="c1">-- Because the metatable has been exposed </span></div><div class='line' id='LC4'><span class="c1">-- to us, we can actually add new functions</span></div><div class='line' id='LC5'><span class="c1">-- to Foo</span></div><div class='line' id='LC6'><span class="k">function</span> <span class="nf">Foo</span><span class="p">:</span><span class="n">speak</span><span class="p">()</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">Hello, I am a Foo&quot;</span><span class="p">)</span></div><div class='line' id='LC8'><span class="k">end</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="kd">local</span> <span class="n">foo</span> <span class="o">=</span> <span class="n">Foo</span><span class="p">.</span><span class="n">new</span><span class="p">(</span><span class="s2">&quot;</span><span class="s">fred&quot;</span><span class="p">)</span></div><div class='line' id='LC11'><span class="kd">local</span> <span class="n">m</span> <span class="o">=</span> <span class="n">foo</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'><span class="c1">-- &quot;fred: 3 + 4 = 7&quot;</span></div><div class='line' id='LC14'><span class="nb">print</span><span class="p">(</span><span class="n">m</span><span class="p">)</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'><span class="c1">-- &quot;Hello, I am a Foo&quot;</span></div><div class='line' id='LC17'><span class="n">foo</span><span class="p">:</span><span class="n">speak</span><span class="p">()</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'><span class="c1">-- Let&#39;s rig the original metatable</span></div><div class='line' id='LC20'><span class="n">Foo</span><span class="p">.</span><span class="n">add_</span> <span class="o">=</span> <span class="n">Foo</span><span class="p">.</span><span class="n">add</span></div><div class='line' id='LC21'><span class="k">function</span> <span class="nf">Foo</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="s2">&quot;</span><span class="s">here comes the magic: &quot;</span> <span class="o">..</span> <span class="n">self</span><span class="p">:</span><span class="n">add_</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span></div><div class='line' id='LC23'><span class="k">end</span></div><div class='line' id='LC24'><br/></div><div class='line' id='LC25'><span class="n">m</span> <span class="o">=</span> <span class="n">foo</span><span class="p">:</span><span class="n">add</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span></div><div class='line' id='LC26'><br/></div><div class='line' id='LC27'><span class="c1">-- &quot;here comes the magic: fred: 9 + 8 = 17&quot;</span></div><div class='line' id='LC28'><span class="nb">print</span><span class="p">(</span><span class="n">m</span><span class="p">)</span></div><div class='line' id='LC29'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1594905/fc362b6cdc0a0bcfc23791857cdcce9f503993f4/fun.lua" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1594905#file_fun.lua" style="float:right;margin-right:10px;color:#666">fun.lua</a>
            <a href="https://gist.github.com/1594905">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/binding-c-classes-to-lua-a-step-by-step-example-for-beginners/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Premake whopping rocks! (or how Boost Build whopping sucks, or how to build Luabind for iOS proper)</title>
		<link>http://cfc.kizzx2.com/index.php/premake-whopping-rocks-or-how-boost-build-whopping-sucks-or-how-to-build-luabind-for-ios-proper/</link>
		<comments>http://cfc.kizzx2.com/index.php/premake-whopping-rocks-or-how-boost-build-whopping-sucks-or-how-to-build-luabind-for-ios-proper/#comments</comments>
		<pubDate>Tue, 10 Jan 2012 15:25:40 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=342</guid>
		<description><![CDATA[Last time I wrote about how to build Luabind on OS X. Well, it turned out that was only half the battle won. Building Luabind for iOS (ARMV7) architecture just plain doesn&#8217;t work. It seems like some people have managed to make it work but none of what I have found actually worked. If you [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fpremake-whopping-rocks-or-how-boost-build-whopping-sucks-or-how-to-build-luabind-for-ios-proper%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fpremake-whopping-rocks-or-how-boost-build-whopping-sucks-or-how-to-build-luabind-for-ios-proper%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>Last time I wrote about how to <a href="http://cfc.kizzx2.com/index.php/building-luabind-on-mac-os-x/">build Luabind on OS X</a>. Well, it turned out that was only half the battle won. Building Luabind for iOS (ARMV7) architecture just plain doesn&#8217;t work. It seems like some people have managed to make it work but none of what I have found actually worked. If you are reading this, I suppose you are also tired of jumping through hoops just to compile the damn thing, well, <a href="http://industriousone.com/premake">Premake</a> to the rescue.</p>

<p>Let me tell you, <em>this is how building stuff in C++ is meant to work</em>:</p>

<p>Just create a file <code>premake4.lua</code></p>

<div class="codecolorer-container lua default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="lua codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">-- premake4.lua</span><br />
<br />
solution <span style="color: #ff6666;">&quot;luabind&quot;</span><br />
&nbsp; &nbsp; configurations <span style="color: #66cc66;">&#123;</span> <span style="color: #ff6666;">&quot;Release&quot;</span> <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; project <span style="color: #ff6666;">&quot;luabind&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; language <span style="color: #ff6666;">&quot;C++&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; kind <span style="color: #ff6666;">&quot;StaticLib&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; flags <span style="color: #66cc66;">&#123;</span> <span style="color: #ff6666;">&quot;Optimize&quot;</span> <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; includedirs <span style="color: #66cc66;">&#123;</span> <span style="color: #ff6666;">&quot;.&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff6666;">&quot;/usr/local/include&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff6666;">&quot;/where/is/your/lua&quot;</span> <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; files <span style="color: #66cc66;">&#123;</span> <span style="color: #ff6666;">&quot;src/**&quot;</span> <span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>

<p>That is ALL THERE IS TO IT!</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ premake4 xcode3<br />
$ xcodebuild <span style="color: #660033;">-sdk</span> iphonesimulator <span style="color: #660033;">-configuration</span> Release <span style="color: #660033;">-arch</span> i386<br />
$ xcodebuild <span style="color: #660033;">-sdk</span> iphoneos <span style="color: #660033;">-configuration</span> Release <span style="color: #660033;">-arch</span> armv7</div></td></tr></tbody></table></div>

<p>Boost Build whopping sucks. Premake is written by one guy and this turns out to be so trivial (as it should be).</p>

<p>Hope it helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/premake-whopping-rocks-or-how-boost-build-whopping-sucks-or-how-to-build-luabind-for-ios-proper/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Building luabind on Mac OS X</title>
		<link>http://cfc.kizzx2.com/index.php/building-luabind-on-mac-os-x/</link>
		<comments>http://cfc.kizzx2.com/index.php/building-luabind-on-mac-os-x/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 15:51:27 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=337</guid>
		<description><![CDATA[1234567891011121314151617181920212223242526272829303132$ # If you have used the Homebrew recipes -- they don't work $ brew uninstall lua $ brew uninstall bjam $ # The current version of LuaBind doesn't compile with Clang. $ Fortunately the GitHub HEAD does. $ git clone https://github.com/luabind/luabind.git $ # LuaBind currently does not compile with Lua 5.2 $ wget $BOOST_URL [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fbuilding-luabind-on-mac-os-x%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fbuilding-luabind-on-mac-os-x%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #666666; font-style: italic;"># If you have used the Homebrew recipes -- they don't work</span><br />
$ brew uninstall lua<br />
$ brew uninstall bjam<br />
<br />
$ <span style="color: #666666; font-style: italic;"># The current version of LuaBind doesn't compile with Clang.</span><br />
$ Fortunately the GitHub HEAD does.<br />
$ <span style="color: #c20cb9; font-weight: bold;">git clone</span> https:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>luabind<span style="color: #000000; font-weight: bold;">/</span>luabind.git<br />
<br />
$ <span style="color: #666666; font-style: italic;"># LuaBind currently does not compile with Lua 5.2</span><br />
$ <span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #007800;">$BOOST_URL</span><br />
$ <span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #007800;">$LUA51_URL</span><br />
$ <span style="color: #c20cb9; font-weight: bold;">wget</span> <span style="color: #007800;">$LUA_URL</span><br />
<br />
$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>where<span style="color: #000000; font-weight: bold;">/</span>is<span style="color: #000000; font-weight: bold;">/</span>boost<br />
$ <span style="color: #c20cb9; font-weight: bold;">chmod</span> a+x tools<span style="color: #000000; font-weight: bold;">/</span>build<span style="color: #000000; font-weight: bold;">/</span>v2<span style="color: #000000; font-weight: bold;">/</span>bootstrap.sh<br />
$ <span style="color: #c20cb9; font-weight: bold;">chmod</span> a+x tools<span style="color: #000000; font-weight: bold;">/</span>build<span style="color: #000000; font-weight: bold;">/</span>v2<span style="color: #000000; font-weight: bold;">/</span>engine<span style="color: #000000; font-weight: bold;">/</span>build.sh<br />
$ .<span style="color: #000000; font-weight: bold;">/</span>bootstrap.sh <span style="color: #007800;">toolset</span>=darwin<br />
$ <span style="color: #c20cb9; font-weight: bold;">cp</span> bjam <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>bin<br />
<br />
$ <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">BOOST_ROOT</span>=<span style="color: #000000; font-weight: bold;">/</span>where<span style="color: #000000; font-weight: bold;">/</span>is<span style="color: #000000; font-weight: bold;">/</span>your<span style="color: #000000; font-weight: bold;">/</span>boost<br />
$ <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">BOOST_BUILD_PATH</span>=<span style="color: #000000; font-weight: bold;">/</span>where<span style="color: #000000; font-weight: bold;">/</span>is<span style="color: #000000; font-weight: bold;">/</span>your<span style="color: #000000; font-weight: bold;">/</span>boost<span style="color: #000000; font-weight: bold;">/</span>tools<span style="color: #000000; font-weight: bold;">/</span>build<span style="color: #000000; font-weight: bold;">/</span>v2<br />
<br />
$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>where<span style="color: #000000; font-weight: bold;">/</span>is<span style="color: #000000; font-weight: bold;">/</span>lua<br />
$ <span style="color: #c20cb9; font-weight: bold;">make</span> macosx<br />
<br />
$ <span style="color: #666666; font-style: italic;"># We need these because LuaBind hard-codes the non-standard directory names</span><br />
$ <span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> src include<br />
<br />
<br />
$ <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LUA_PATH</span>=<span style="color: #000000; font-weight: bold;">/</span>where<span style="color: #000000; font-weight: bold;">/</span>is<span style="color: #000000; font-weight: bold;">/</span>lua<br />
$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>where<span style="color: #000000; font-weight: bold;">/</span>is<span style="color: #000000; font-weight: bold;">/</span>luabind<br />
$ bjam <span style="color: #007800;">toolset</span>=darwin <span style="color: #007800;">link</span>=static</div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/building-luabind-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BOOTMGR is missing &#8212; totally demytisfied</title>
		<link>http://cfc.kizzx2.com/index.php/bootmgr-is-missing-totally-demytisfied/</link>
		<comments>http://cfc.kizzx2.com/index.php/bootmgr-is-missing-totally-demytisfied/#comments</comments>
		<pubDate>Tue, 27 Dec 2011 14:23:15 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=328</guid>
		<description><![CDATA[A couple of years back I blogged about how I solved the frustrating and notorious &#8220;BOOTMGR is missing&#8221; error in Windows. Today I encountered it again and this time it seems to be more tenacious but I still tamed it after numerous trials. Here I document exactly what went wrong, why it happened and how [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fbootmgr-is-missing-totally-demytisfied%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fbootmgr-is-missing-totally-demytisfied%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>A couple of years back I blogged about <a href="http://kizzx2.com/blog/index.php/2008/07/08/bootmgr-is-missing-press-ctrlaltdel-to-restart/">how I solved</a> the frustrating and notorious &#8220;BOOTMGR is missing&#8221; error in Windows. Today I encountered it again and this time it seems to be more tenacious but I still tamed it after numerous trials. Here I document exactly what went wrong, why it happened and how to solve it.</p>

<h2>Background information &#8212; how does Windows boot?</h2>

<p>Here is a quick rundown. Every step below could possibly go wrong:</p>

<ol>
<li><p>The BIOS loads the MBR of the booting disk by handing over control to the code residing there. MS&#8217;s MBR code will then load the boot sector of the active partition. (If something is wrong here, we need <code>bootsect /nt60 X: /mbr</code>).</p></li>
<li><p>The boot sector looks for <code>\Boot\BCD</code> and loads boot information from there. The BCD contains where BOOTMGR resides. If this step is wrong, you will see the infamous &#8220;BOOTMGR is missing&#8221;. To fix this, we need to correct the BCD by <code>bcdedit</code> (see below).</p></li>
<li><p>Finally BOOTMGR loads <code>winload.exe</code>. If the <code>device</code> or <code>osdevice</code> is set to the wrong <code>partition</code>, Windows may report that <code>winload.exe</code> is corrupt. Normally you fix this by <code>bcdedit</code>. In the rare events that <code>winload.exe</code> is really corrupt, you may need to do a Windows Repair.</p></li>
</ol>

<h2>Why did it happen?</h2>

<p>The base case for this to happen is a multi-hard disk computer. Variations may include changing hard disks after installation, but the principle is the same.</p>

<p>Suppose we have a computer with 2 hard disks:</p>

<ol>
<li>HDD1 (SATA 1/IDE 1)</li>
<li>HDD2 (SATA 2/IDE 2)</li>
</ol>

<p>That is, HDD1 comes before HDD2 in BIOS.</p>

<p>Since Windows cannot determine your boot order from BIOS, it will <strong>always</strong> configures the MBR in HDD1, and configures the boot sector of the first partition of HDD1 and also put <code>Boot\BCD</code> in the first partition of HDD1. It will always be HDD1, no matter which disk you decide to install Windows on.</p>

<p>Suppose you have the following <strong>boot order</strong> in BIOS:</p>

<ol>
<li>HDD2</li>
<li>HDD1</li>
</ol>

<p>And suppose you have installed Windows so that:</p>

<ul>
<li>HDD1 &#8212; bootloader code</li>
<li>HDD2 &#8212; actual Windows installation</li>
</ul>

<p>Then booting will fail and you will see the notorious BOOTMGR is missing error. Another reason you may see this is that you remove HDD1 after some time, thinking that your Windows installation is nicely contained in HDD2.</p>

<h2>Why is Windows Repair not enough?</h2>

<p>If you use Winodws repair it will install the boot code in HDD1 and you are forced to adjust the BIOS to boot HDD2 through HDD1. For me, it was a purist&#8217;s reason to want to move everything to HDD2.</p>

<p>So here is how exactly I tacked it.</p>

<h2>How to fix it</h2>

<p>The following steps are assumed to be done in the Windows Recovery environment command line. Boot up your Windows CD and press <code>Shift+F10</code> to call up the command prompt. We will assume:</p>

<ul>
<li><code>E:</code> to be where Windows is installed.</li>
<li><code>F:</code> to be where BCD is set up.</li>
<li><code>X:</code> to be the Windows installation CD</li>
</ul>

<p>&nbsp;1. First, make sure you set the Windows partition as active. You can do this using various partition software or just Google how to do it. Here is an example session using <code>diskpart</code>. You need to replace 99 with the correct numbers:</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&gt; diskpart<br />
&gt; list disk<br />
&gt; select disk 99<br />
&gt; list partition<br />
&gt; select partition 99<br />
&gt; active<br />
&gt; exit</div></td></tr></tbody></table></div>

<p>&nbsp;2. Now we gotta make sure the MBR of HDD2 is good to go:</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&gt; cd /d X:\Boot<br />
&gt; bootsect /nt60 E: /mbr</div></td></tr></tbody></table></div>

<p>&nbsp;3. Now migrate <code>BOOTMGR</code> and <code>Boot</code> from <code>F:</code> to <code>E:</code></p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&gt; cd /d F:\<br />
&gt; attrib -s -e -h bootmgr<br />
&gt; move bootmgr E:\<br />
&gt; move Boot E:\<br />
&gt; cd /d E:\<br />
&gt; attrib +s +e +h bootmgr</div></td></tr></tbody></table></div>

<p>&nbsp;4. Now use <code>bcdedit</code> to fix up the BCD</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&gt; cd /d E:\Boot<br />
&gt; bcdedit /store BCD /set {bootmgr} device partition=E:<br />
&gt; bcdedit /store BCD /set {default} device partition=E:<br />
&gt; bcdedit /store BCD /set {default} osdevice partition=E:</div></td></tr></tbody></table></div>

<p>&nbsp;5. That&#8217;s it!</p>

<h2>How to fix it from scratch (what I actually did)</h2>

<p>If in the process you think you have screwed up so bad that you want to start over, you don&#8217;t have to reinstall Windows:</p>

<ol>
<li><p>It is save to wife out both <code>BOOTMGR</code> and the <code>Boot</code> directory. You can find <code>BOOTMGR</code> in the Windows install CD.</p></li>
<li><p>To recreate the <code>Boot</code> directory, run <code>bootrec /rebuildbcd</code>. Windows will insist on creating it in the first partition in HDD1. You can apply the above techniques to patch up the newly created BCD and then move it to <code>E:</code>. Alternatively <a href="http://msdn.microsoft.com/en-us/library/windows/hardware/ff541231(v=vs.85).aspx">you can create BCD from real scratch</a> by using the UUIDs from <code>bcdedit /enum all</code>.</p></li>
<li><p>You should be able to get Windows booted with <strong>just</strong> the two files <code>E:\BOOTMGR</code> and <code>E:\Boot\BCD</code>. If that worked you will find that Windows boots with Vista style&#8217;s green bar. To fix it:</p></li>
</ol>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&gt; bcdedit /store E:\Boot\BCD /set {bootmgr} locale en-US<br />
&gt; xcopy /e /h X:\Boot\en-us\ E:\Boot\en-us\</div></td></tr></tbody></table></div>

<p>That should be it. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/bootmgr-is-missing-totally-demytisfied/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Irrlicht vs. Ogre for mobile game development</title>
		<link>http://cfc.kizzx2.com/index.php/irrlicht-vs-ogre-for-mobile-game-development/</link>
		<comments>http://cfc.kizzx2.com/index.php/irrlicht-vs-ogre-for-mobile-game-development/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 16:55:19 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=323</guid>
		<description><![CDATA[I have been evaluating several 3D frameworks for cross-platform mobile game development lately. Here&#8217;s my findings. To stay objective, I am just going to state a couple of hard facts as conclusion: Irrlicht is smaller than Ogre. I did an actual experiment using both. I compiled both into static libraries (required for an iPhone deployment). [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Firrlicht-vs-ogre-for-mobile-game-development%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Firrlicht-vs-ogre-for-mobile-game-development%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>I have been evaluating several 3D frameworks for cross-platform mobile game development lately. Here&#8217;s my findings. To stay objective, I am just going to state a couple of hard facts as conclusion:</p>

<ul>
<li><p>Irrlicht is smaller than Ogre. I did an actual experiment using both. I compiled both into static libraries (required for an iPhone deployment). A &#8220;Hello World&#8221; app with just <em>one line</em> of <code>irr::createDevice</code> (to get stuff statically linked) results in an 4MB executable; the same app with one line of <code>new Ogre::Root</code> results in an 6.5MB executable. The Ogre one is already heavily optimized by disabling FreeImage and some other components. (For the record, Cocos3D&#8217;s &#8220;Hello World&#8221; app demo is 2.5MB, but it is not cross platform at the moment).</p></li>
<li><p>The Ogre-iPhone &#8220;branch&#8221; is official; the Irrlicht OGL-ES branch is not official (yet).</p></li>
<li><p>Ogre-iPhone can do shader programming (OpenGL ES 2.0). There are OpenGL ES 2.0 files in the Irrlicht OGL-ES branch but I have not played with it and don&#8217;t know how mature it is.</p></li>
</ul>

<p>That&#8217;s it. The size point might not be that relevant for desktop development but definitely is when it comes to small game developments. I actually prefer Ogre&#8217;s &#8220;more complicated&#8221; &#8220;syntax&#8221; (a much touted disadvantage by Irrlicht&#8217;s community) better but for this one I think I&#8217;m going with Irrlicht.</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/irrlicht-vs-ogre-for-mobile-game-development/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Sencha Touch vs jQuery Mobile &#8212; a first look</title>
		<link>http://cfc.kizzx2.com/index.php/sencha-touch-vs-jquery-mobile-a-first-look/</link>
		<comments>http://cfc.kizzx2.com/index.php/sencha-touch-vs-jquery-mobile-a-first-look/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 16:19:44 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=317</guid>
		<description><![CDATA[Most people compare Sencha Touch to jQuery Mobile. My general impression from reading the Web is that Sencha is more heavy-duty, faster than jQuery. My actual experience, however, reveals that they are actually vastly different. I recently started using PhoneGap to do mobile development. The idea of using Web technology to write cross platform apps [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fsencha-touch-vs-jquery-mobile-a-first-look%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fsencha-touch-vs-jquery-mobile-a-first-look%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>Most people compare Sencha Touch to jQuery Mobile. My general impression from reading the Web is that Sencha is more heavy-duty, faster than jQuery. My actual experience, however, reveals that they are actually <em>vastly different</em>.</p>

<p>I recently started using <a href="http://www.phonegap.com/">PhoneGap</a> to do mobile development. The idea of using Web technology to write cross platform apps is just so great!</p>

<p>The natural thing to do with PhoneGap would be to use a framework. There are many, just to name a few that I tried:</p>

<ul>
<li><a href="http://xuijs.com/">xui</a>: Lightweight and fast, just as what you&#8217;d expect &#8212; jQuery on diet. I am not sure I am unlocking its real potential since examples/demos are non-existent at the time I write this. But this gets the job done.</li>
<li><a href="http://www.sencha.com/products/touch/">Senca Touch</a>: Claims to be &#8220;the best HTML5 mobile Web app framework&#8221;. The demos are polished and attractive.</li>
<li><a href="http://jquerymobile.com/">jQuery Mobie</a>: Good old familiar jQuery. The demos feel a little sluggish but overall still look good. It has less &#8220;widgets&#8221; than Sencha.</li>
</ul>

<p>So let me get to the point of this post: I tried Sencha first and then tried jQuery. I will tell you that Sencha has slightly better performance but it&#8217;s approach really disappointed me.</p>

<p>Let me quote a snippet of Sencha Touch code from <a href="http://www.sencha.com/learn/a-sencha-touch-mvc-application-with-phonegap/">the PhoneGap tutorial on its Web site</a>. This is how a typical Sencha Touch &#8220;class&#8221; look like:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">app.<span style="color: #660066;">views</span>.<span style="color: #660066;">ContactDetail</span> <span style="color: #339933;">=</span> Ext.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>Ext.<span style="color: #660066;">Panel</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; dockedItems<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; xtype<span style="color: #339933;">:</span> <span style="color: #3366CC;">'toolbar'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; title<span style="color: #339933;">:</span> <span style="color: #3366CC;">'View contact'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; items<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; text<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Back'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ui<span style="color: #339933;">:</span> <span style="color: #3366CC;">'back'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; listeners<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'tap'</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//Ext.dispatch({</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp;controller: app.controllers.contacts,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp;action: 'index',</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp;animation: {type:'slide', direction:'right'}</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//});</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span>xtype<span style="color: #339933;">:</span><span style="color: #3366CC;">'spacer'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; id<span style="color: #339933;">:</span> <span style="color: #3366CC;">'edit'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; text<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Edit'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ui<span style="color: #339933;">:</span> <span style="color: #3366CC;">'action'</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; listeners<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #3366CC;">'tap'</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//Ext.dispatch({</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp;controller: app.controllers.contacts,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp;action: 'edit',</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// &nbsp;id: this.record.getId()</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//});</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#93;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; styleHtmlContent<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
&nbsp; <span style="color: #000066;">scroll</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'vertical'</span><span style="color: #339933;">,</span><br />
&nbsp; items<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<p>Whew, that&#8217;s quite a lot of stuffs. Let me compare that with a selected part of a <a href="http://mobile.tutsplus.com/tutorials/mobile-web-apps/jquery-mobile-forms/">jQuery Mobile tutorial</a>:</p>

<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#buttonOK'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; hideContentDialog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; showMain<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp;<br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#form1'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> err <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Hide the Main content</span><br />
&nbsp; &nbsp; hideMain<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Reset the previously highlighted form elements</span><br />
&nbsp; &nbsp; stateLabelVar.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span>MISSING<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; whatLabelVar.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span>MISSING<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; inputMapVar.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span>MISSING<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Perform form validation</span><br />
&nbsp; &nbsp; inputMapVar.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">||</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span>EMPTY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prev</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>MISSING<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; err <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>stateVar.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span>NO_STATE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; stateLabelVar.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>MISSING<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; err <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>whatVar.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">||</span>whatVar.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span>EMPTY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; <br />
&nbsp; &nbsp; &nbsp; whatLabelVar.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span>MISSING<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> &nbsp; <br />
&nbsp; &nbsp; &nbsp; err <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// If validation fails, show Dialog content</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>err <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; showContentDialog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// If validation passes, show Transition content</span><br />
&nbsp; &nbsp; showContentTransition<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// Submit the form</span><br />
&nbsp; &nbsp; $.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/forms/requestProcessor.php&quot;</span><span style="color: #339933;">,</span> form1Var.<span style="color: #660066;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; confirmationVar.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; hideContentTransition<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; showConfirmation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> &nbsp; &nbsp; &nbsp;<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>

<p>This looks so familiar. It&#8217;s just our good old jQuery. <strong>And the page was formatted with standard, vanilla HTML5</strong>.</p>

<p>The difference should now be apparent &#8212; jQuery Mobile is a real &#8220;moving the Web development experience to mobile platforms&#8221;, whereas Sencha is &#8220;moving ExtJS to the mobile platform&#8221;.</p>

<p>If you look at the code, you&#8217;ll find that the typical Sencha Touch application bears no resemblance to how a Web app looks like &#8212; it feels like writing Java view classes. Unless you were already an ExtJS expert, it&#8217;s like going to learn a whole different ecosystem. Maybe it&#8217;s just me, but I find that <em>way</em> more difficult to adjust the layout than HTML or using XIB GUI editor. Writing layouts in &#8220;plain text&#8221; feels like something from the 90&#8242;s, seriously. Every other GUI toolkit I know of has a GUI editor, and plain HTML has the advantage of being ubiqutous. I can already imagine tearing the manual while writing GUI in Sencha Touch &#8212; not particularly attractive.</p>

<p>Using jQuery Mobile, links are automatically &#8220;hijaxed&#8221; into AJAX-y links, without you having to define these &#8216;tap&#8217; actions whatsoever. The other good thing about it is that it works in a desktop browser by default, whereas Sencha Touch&#8217;s tutorial app just gives me a blank page when viewed from Firefox.</p>

<p>My general feeling is that if I am going to use yet another framework, I may as well go native or use Titanium which gives me native performance. For the moment I am going to stick with jQuery Mobile.</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/sencha-touch-vs-jquery-mobile-a-first-look/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>MiKTeX + XeTeX &#8220;\char_make_active:n {&#8220;20}%&#8221; problem and the solution!</title>
		<link>http://cfc.kizzx2.com/index.php/miktex-xetex-char_make_activen-20-problem-and-the-solution/</link>
		<comments>http://cfc.kizzx2.com/index.php/miktex-xetex-char_make_activen-20-problem-and-the-solution/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 13:28:28 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=315</guid>
		<description><![CDATA[You need to update all your packages. The latest MiKTeX package manager seems rather broken. If you use Task > Update Wizard, you&#8217;ll probably meet &#8220;The update wizard could not be found&#8221; The solution is to open the Update program directly (type &#8220;Update&#8221; in Start Menu or find miktex-update.exe. I used both the normal variant [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fmiktex-xetex-char_make_activen-20-problem-and-the-solution%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fmiktex-xetex-char_make_activen-20-problem-and-the-solution%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>You need to update all your packages. The latest MiKTeX package manager seems rather broken. If you use Task > Update Wizard, you&#8217;ll probably meet &#8220;The update wizard could not be found&#8221;</p>

<p>The solution is to open the Update program directly (type &#8220;Update&#8221; in Start Menu or find <code>miktex-update.exe</code>. I used both the normal variant and the &#8220;(Admin)&#8221; variant. A couple of updates later it&#8217;s finally working!</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/miktex-xetex-char_make_activen-20-problem-and-the-solution/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Illustrated Haskell</title>
		<link>http://cfc.kizzx2.com/index.php/illustrated-haskell/</link>
		<comments>http://cfc.kizzx2.com/index.php/illustrated-haskell/#comments</comments>
		<pubDate>Sat, 24 Sep 2011 10:13:06 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=311</guid>
		<description><![CDATA[I launched a new site blog Illustrated Haskell where I experiment with a new approach to explain intermediate Haskell concepts, inspired by Khan Academy&#8216;s style. I am not doing videos (yet?) but the style is intended to be narrative and easy to follow. Check it out]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fillustrated-haskell%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fillustrated-haskell%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>I launched a new site blog <a href="http://illustratedhaskell.org/">Illustrated Haskell</a> where I experiment with a new approach to explain intermediate Haskell concepts, inspired by <a href="http://www.khanacademy.org/">Khan Academy</a>&#8216;s style. I am not doing videos (yet?) but the style is intended to be narrative and easy to follow.</p>

<p>Check it out <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/illustrated-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>In Search of Performance in Haskell</title>
		<link>http://cfc.kizzx2.com/index.php/in-search-of-performance-in-haskell/</link>
		<comments>http://cfc.kizzx2.com/index.php/in-search-of-performance-in-haskell/#comments</comments>
		<pubDate>Tue, 09 Aug 2011 16:21:19 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[haskell performance ghc]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=294</guid>
		<description><![CDATA[I was doing ITA Software&#8217;s &#8220;Word Numbers&#8221; problem. Now there are various solutions on the Web. Reviewing those solutions tell us it&#8217;s a graph/grammar problem. Today we&#8217;re going to solve it brute-force. Now let me give a quick recap of the problem: A "word number" is defined as wordNumber 1 = "one" wordNumber 2 = [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fin-search-of-performance-in-haskell%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fin-search-of-performance-in-haskell%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>I was doing <a href="http://www.itasoftware.com/careers/puzzle_archive.html">ITA Software&#8217;s &#8220;Word Numbers&#8221;</a> problem. Now there are <a href="http://google.com/search?q=ita+word+numbers">various solutions on the Web</a>. Reviewing those solutions tell us it&#8217;s a graph/grammar problem. Today we&#8217;re going to solve it brute-force.</p>

<p>Now let me give a quick recap of the problem:</p>

<p><pre>
A "word number" is defined as</p>

<p>wordNumber 1 = "one"
wordNumber 2 = "onetwo"
wordNumber 3 = "onetwothree"
...
wordNumber 12 = "onetwothreefourfivesixseveneightnineteneleventwelve"
...</p>

<p>Find the 51-billion-th letter of the <code>wordNumber Infinity</code>. Assume that letter is found for <code>wordNumber x</code>, also find the sum of 1 to <code>x</code>
</pre></p>

<p>(As someone noted in the StackOverflow comments, I actually misinterpreted the original problem. But that doesn&#8217;t affect our purpose here.)</p>

<h2>A naive algorithm in C++</h2>

<p>The idea seems simple: 2 counters, one for sum of numbers and one for length. We go from <code>wordNumber 1</code> until the length meets 51000000000. Here is how I said it in C++:</p>

<div id="gist-1134262" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">// Brute force solver for the Word Number problem</span></div><div class='line' id='LC2'>&nbsp;</div><div class='line' id='LC3'><span class="cp">#include &lt;iostream&gt;</span></div><div class='line' id='LC4'><span class="cp">#include &lt;string&gt;</span></div><div class='line' id='LC5'>&nbsp;</div><div class='line' id='LC6'><span class="kt">int</span> <span class="n">length_ones</span><span class="p">[</span><span class="mi">10</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">};</span> <span class="c1">// &quot;&quot;, one, two, three, ...</span></div><div class='line' id='LC7'><span class="kt">int</span> <span class="n">length_tens</span><span class="p">[</span><span class="mi">10</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">};</span> <span class="c1">// &quot;&quot;, ten, twenty, ...</span></div><div class='line' id='LC8'><span class="kt">int</span> <span class="n">length_teens</span><span class="p">[</span><span class="mi">10</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">3</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">8</span><span class="p">};</span> <span class="c1">// ten, eleven, twelve, ...</span></div><div class='line' id='LC9'>&nbsp;</div><div class='line' id='LC10'><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">ones</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="s">&quot;one&quot;</span><span class="p">,</span> <span class="s">&quot;two&quot;</span><span class="p">,</span> <span class="s">&quot;three&quot;</span><span class="p">,</span> <span class="s">&quot;four&quot;</span><span class="p">,</span> <span class="s">&quot;five&quot;</span><span class="p">,</span> <span class="s">&quot;six&quot;</span><span class="p">,</span> <span class="s">&quot;seven&quot;</span><span class="p">,</span> <span class="s">&quot;eight&quot;</span><span class="p">,</span> <span class="s">&quot;nine&quot;</span><span class="p">};</span></div><div class='line' id='LC11'><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">tens</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="s">&quot;ten&quot;</span><span class="p">,</span> <span class="s">&quot;twenty&quot;</span><span class="p">,</span> <span class="s">&quot;thirty&quot;</span><span class="p">,</span> <span class="s">&quot;forty&quot;</span><span class="p">,</span> <span class="s">&quot;fifty&quot;</span><span class="p">,</span> <span class="s">&quot;sixty&quot;</span><span class="p">,</span> <span class="s">&quot;seventy&quot;</span><span class="p">,</span> <span class="s">&quot;eighty&quot;</span><span class="p">,</span> <span class="s">&quot;ninety&quot;</span><span class="p">};</span></div><div class='line' id='LC12'><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">teens</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;ten&quot;</span><span class="p">,</span> <span class="s">&quot;eleven&quot;</span><span class="p">,</span> <span class="s">&quot;twelve&quot;</span><span class="p">,</span> <span class="s">&quot;thirteen&quot;</span><span class="p">,</span> <span class="s">&quot;fourteen&quot;</span><span class="p">,</span> <span class="s">&quot;fifteen&quot;</span><span class="p">,</span> <span class="s">&quot;sixteen&quot;</span><span class="p">,</span> <span class="s">&quot;seventeen&quot;</span><span class="p">,</span> <span class="s">&quot;eighteeen&quot;</span><span class="p">,</span> <span class="s">&quot;nineteen&quot;</span><span class="p">};</span></div><div class='line' id='LC13'>&nbsp;</div><div class='line' id='LC14'><span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">wordify</span><span class="p">(</span><span class="kt">long</span> <span class="n">n</span><span class="p">)</span></div><div class='line' id='LC15'><span class="p">{</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span><span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="p">)</span> <span class="k">return</span> <span class="n">ones</span><span class="p">[</span><span class="n">n</span><span class="p">];</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="mi">20</span><span class="p">)</span> <span class="k">return</span> <span class="n">teens</span><span class="p">[</span><span class="n">n</span><span class="o">-</span><span class="mi">10</span><span class="p">];</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="mi">100</span><span class="p">)</span> <span class="k">return</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="p">(</span><span class="n">tens</span><span class="p">[</span><span class="n">n</span><span class="o">/</span><span class="mi">10</span><span class="p">])</span> <span class="o">+</span> <span class="n">ones</span><span class="p">[</span><span class="n">n</span><span class="o">%</span><span class="mi">10</span><span class="p">];</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1000</span><span class="p">)</span> <span class="k">return</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="p">(</span><span class="n">ones</span><span class="p">[</span><span class="n">n</span><span class="o">/</span><span class="mi">100</span><span class="p">])</span> <span class="o">+</span> <span class="s">&quot;hundred&quot;</span> <span class="o">+</span> <span class="n">wordify</span><span class="p">(</span><span class="n">n</span><span class="o">%</span><span class="mi">100</span><span class="p">);</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1000000</span><span class="p">)</span> <span class="k">return</span> <span class="n">wordify</span><span class="p">(</span><span class="n">n</span><span class="o">/</span><span class="mi">1000</span><span class="p">)</span> <span class="o">+</span> <span class="s">&quot;thousand&quot;</span> <span class="o">+</span> <span class="n">wordify</span><span class="p">(</span><span class="n">n</span><span class="o">%</span><span class="mi">1000</span><span class="p">);</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span> <span class="k">return</span> <span class="n">wordify</span><span class="p">(</span><span class="n">n</span><span class="o">/</span><span class="mi">1000000</span><span class="p">)</span> <span class="o">+</span> <span class="s">&quot;million&quot;</span> <span class="o">+</span> <span class="n">wordify</span><span class="p">(</span><span class="n">n</span><span class="o">%</span><span class="mi">1000000</span><span class="p">);</span></div><div class='line' id='LC22'><span class="p">}</span></div><div class='line' id='LC23'>&nbsp;</div><div class='line' id='LC24'><span class="kt">int</span> <span class="n">word_length</span><span class="p">(</span><span class="kt">long</span> <span class="n">n</span><span class="p">)</span></div><div class='line' id='LC25'><span class="p">{</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span><span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="mi">10</span><span class="p">)</span> <span class="k">return</span> <span class="n">length_ones</span><span class="p">[</span><span class="n">n</span><span class="p">];</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="mi">20</span><span class="p">)</span> <span class="k">return</span> <span class="n">length_teens</span><span class="p">[</span><span class="n">n</span><span class="o">-</span><span class="mi">10</span><span class="p">];</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="mi">100</span><span class="p">)</span> <span class="k">return</span> <span class="n">length_tens</span><span class="p">[</span><span class="n">n</span><span class="o">/</span><span class="mi">10</span><span class="p">]</span> <span class="o">+</span> <span class="n">length_ones</span><span class="p">[</span><span class="n">n</span><span class="o">%</span><span class="mi">10</span><span class="p">];</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1000</span><span class="p">)</span> <span class="k">return</span> <span class="n">length_ones</span><span class="p">[</span><span class="n">n</span><span class="o">/</span><span class="mi">100</span><span class="p">]</span> <span class="o">+</span> <span class="mi">7</span> <span class="o">+</span> <span class="n">word_length</span><span class="p">(</span><span class="n">n</span><span class="o">%</span><span class="mi">100</span><span class="p">);</span> <span class="c1">// 7 for &quot;hundred&quot;</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1000000</span><span class="p">)</span> <span class="k">return</span> <span class="n">word_length</span><span class="p">(</span><span class="n">n</span><span class="o">/</span><span class="mi">1000</span><span class="p">)</span> <span class="o">+</span> <span class="mi">8</span> <span class="o">+</span> <span class="n">word_length</span><span class="p">(</span><span class="n">n</span><span class="o">%</span><span class="mi">1000</span><span class="p">);</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span> <span class="k">return</span> <span class="n">word_length</span><span class="p">(</span><span class="n">n</span><span class="o">/</span><span class="mi">1000000</span><span class="p">)</span> <span class="o">+</span> <span class="mi">7</span> <span class="o">+</span> <span class="n">word_length</span><span class="p">(</span><span class="n">n</span><span class="o">%</span><span class="mi">1000000</span><span class="p">);</span></div><div class='line' id='LC32'><span class="p">}</span></div><div class='line' id='LC33'>&nbsp;</div><div class='line' id='LC34'><span class="kt">int</span> <span class="n">main</span><span class="p">()</span></div><div class='line' id='LC35'><span class="p">{</span></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">long</span> <span class="n">sumNumbers</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span></div><div class='line' id='LC37'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">long</span> <span class="n">sumLength</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span></div><div class='line' id='LC38'>&nbsp;</div><div class='line' id='LC39'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">const</span> <span class="kt">long</span> <span class="n">target</span> <span class="o">=</span> <span class="mi">51000000000</span><span class="p">;</span></div><div class='line' id='LC40'>&nbsp;</div><div class='line' id='LC41'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">for</span><span class="p">(</span><span class="kt">long</span> <span class="n">i</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="mi">999999999</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span></div><div class='line' id='LC42'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">{</span></div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">sumNumbers</span> <span class="o">+=</span> <span class="n">i</span><span class="p">;</span></div><div class='line' id='LC44'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">long</span> <span class="n">newSumLength</span> <span class="o">=</span> <span class="n">word_length</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="o">+</span> <span class="n">sumLength</span><span class="p">;</span></div><div class='line' id='LC45'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span><span class="p">(</span><span class="n">newSumLength</span> <span class="o">&gt;=</span> <span class="n">target</span><span class="p">)</span></div><div class='line' id='LC46'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">break</span><span class="p">;</span></div><div class='line' id='LC47'>&nbsp;</div><div class='line' id='LC48'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">sumLength</span> <span class="o">=</span> <span class="n">newSumLength</span><span class="p">;</span></div><div class='line' id='LC49'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC50'>&nbsp;</div><div class='line' id='LC51'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;Sum: &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">sumNumbers</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span></div><div class='line' id='LC52'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;The letter is &quot;</span> <span class="o">&lt;&lt;</span> <span class="n">wordify</span><span class="p">(</span><span class="n">i</span><span class="p">)[</span><span class="n">target</span> <span class="o">-</span> <span class="n">sumLength</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span></div><div class='line' id='LC53'>&nbsp;</div><div class='line' id='LC54'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="mi">0</span><span class="p">;</span></div><div class='line' id='LC55'><span class="p">}</span></div><div class='line' id='LC56'>&nbsp;</div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1134262/17855cde70c9250ab0c5d2b40a3f136eece5932d/WordNumber.cpp" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1134262#file_word_number.cpp" style="float:right;margin-right:10px;color:#666">WordNumber.cpp</a>
            <a href="https://gist.github.com/1134262">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>


<p>OK, nothing surprising. It takes <strong>45 seconds</strong> on my machine to execute (<code>g++ -O3</code>). We will use this as a baseline.</p>

<h2>First attempt in Haskell</h2>

<div id="gist-1134262" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">-- Original version</span></div><div class='line' id='LC2'><span class="c1">-- Can you spot how to improve this performance by 3 fold right away?</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><span class="cm">{-# LANGUAGE BangPatterns #-}</span></div><div class='line' id='LC5'>&nbsp;</div><div class='line' id='LC6'><span class="kr">import</span> <span class="nn">Debug.Trace</span></div><div class='line' id='LC7'><span class="kr">import</span> <span class="nn">Data.Int</span></div><div class='line' id='LC8'><span class="kr">import</span> <span class="nn">Control.Monad</span></div><div class='line' id='LC9'><span class="kr">import</span> <span class="nn">Data.Array.Unboxed</span></div><div class='line' id='LC10'>&nbsp;</div><div class='line' id='LC11'><span class="nf">ones</span> <span class="ow">=</span> <span class="p">[</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="s">&quot;one&quot;</span><span class="p">,</span> <span class="s">&quot;two&quot;</span><span class="p">,</span> <span class="s">&quot;three&quot;</span><span class="p">,</span> <span class="s">&quot;four&quot;</span><span class="p">,</span> <span class="s">&quot;five&quot;</span><span class="p">,</span> <span class="s">&quot;six&quot;</span><span class="p">,</span> <span class="s">&quot;seven&quot;</span><span class="p">,</span> <span class="s">&quot;eight&quot;</span><span class="p">,</span> <span class="s">&quot;nine&quot;</span><span class="p">]</span></div><div class='line' id='LC12'><span class="nf">tens</span> <span class="ow">=</span> <span class="p">[</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="s">&quot;ten&quot;</span><span class="p">,</span> <span class="s">&quot;twenty&quot;</span><span class="p">,</span> <span class="s">&quot;thirty&quot;</span><span class="p">,</span> <span class="s">&quot;forty&quot;</span><span class="p">,</span> <span class="s">&quot;fifty&quot;</span><span class="p">,</span> <span class="s">&quot;sixty&quot;</span><span class="p">,</span> <span class="s">&quot;seventy&quot;</span><span class="p">,</span> <span class="s">&quot;eighty&quot;</span><span class="p">,</span> <span class="s">&quot;ninety&quot;</span><span class="p">]</span></div><div class='line' id='LC13'><span class="nf">teens</span> <span class="ow">=</span> <span class="p">[</span><span class="s">&quot;ten&quot;</span><span class="p">,</span> <span class="s">&quot;eleven&quot;</span><span class="p">,</span> <span class="s">&quot;twelve&quot;</span><span class="p">,</span> <span class="s">&quot;thirteen&quot;</span><span class="p">,</span> <span class="s">&quot;fourteen&quot;</span><span class="p">,</span> <span class="s">&quot;fifteen&quot;</span><span class="p">,</span> <span class="s">&quot;sixteen&quot;</span><span class="p">,</span> <span class="s">&quot;seventeen&quot;</span><span class="p">,</span> <span class="s">&quot;eighteen&quot;</span><span class="p">,</span> <span class="s">&quot;nineteen&quot;</span><span class="p">]</span></div><div class='line' id='LC14'>&nbsp;</div><div class='line' id='LC15'><span class="nf">lenOnes</span><span class="p">,</span> <span class="n">lenTens</span><span class="p">,</span> <span class="n">lenTeens</span> <span class="ow">::</span> <span class="kt">UArray</span> <span class="kt">Int64</span> <span class="kt">Int64</span></div><div class='line' id='LC16'><span class="nf">lenOnes</span> <span class="ow">=</span> <span class="n">listArray</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">9</span><span class="p">)</span> <span class="o">$</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">]</span> <span class="c1">-- &quot;&quot;, &quot;one&quot;,&quot;two&quot;, ...</span></div><div class='line' id='LC17'><span class="nf">lenTens</span> <span class="ow">=</span> <span class="n">listArray</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">9</span><span class="p">)</span> <span class="o">$</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">]</span></div><div class='line' id='LC18'><span class="nf">lenTeens</span> <span class="ow">=</span> <span class="n">listArray</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">9</span><span class="p">)</span> <span class="o">$</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">8</span><span class="p">]</span> <span class="c1">-- first element is &quot;ten&quot; 3</span></div><div class='line' id='LC19'>&nbsp;</div><div class='line' id='LC20'><span class="c1">-- potentially cleaner version</span></div><div class='line' id='LC21'><span class="c1">-- but I feared Haskell might do surprising things behind my </span></div><div class='line' id='LC22'><span class="c1">-- back so I stuck with the above</span></div><div class='line' id='LC23'><span class="c1">-- lenBelowHundred = listArray (0,99) $ map (fromIntegral . length . wordify) [1..99]</span></div><div class='line' id='LC24'>&nbsp;</div><div class='line' id='LC25'><span class="c1">-- wordify 123 = &quot;onehundredtwentythree&quot;</span></div><div class='line' id='LC26'><span class="c1">-- This is only used once in presenting the final result character</span></div><div class='line' id='LC27'><span class="nf">wordify</span> <span class="ow">::</span> <span class="kt">Int64</span> <span class="ow">-&gt;</span> <span class="kt">String</span></div><div class='line' id='LC28'><span class="nf">wordify</span> <span class="n">n</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">10</span>         <span class="ow">=</span> <span class="n">ones</span> <span class="o">!!</span> <span class="n">fromIntegral</span> <span class="n">n</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">20</span>         <span class="ow">=</span> <span class="n">teens</span> <span class="o">!!</span> <span class="p">(</span><span class="n">fromIntegral</span> <span class="n">n</span><span class="o">-</span><span class="mi">10</span><span class="p">)</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">100</span>        <span class="ow">=</span> <span class="n">splitterTen</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1000</span>       <span class="ow">=</span> <span class="n">splitter</span> <span class="mi">100</span> <span class="s">&quot;hundred&quot;</span></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1000000</span>    <span class="ow">=</span> <span class="n">splitter</span> <span class="mi">1000</span> <span class="s">&quot;thousand&quot;</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1000000000</span> <span class="ow">=</span> <span class="n">splitter</span> <span class="mi">1000000</span> <span class="s">&quot;million&quot;</span></div><div class='line' id='LC35'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">where</span></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">splitterTen</span> <span class="ow">=</span> <span class="kr">let</span> <span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span> <span class="ow">=</span> <span class="n">n</span> <span class="p">`</span><span class="n">divMod</span><span class="p">`</span> <span class="mi">10</span></div><div class='line' id='LC37'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">in</span> <span class="p">(</span><span class="n">tens</span> <span class="o">!!</span> <span class="n">fromIntegral</span> <span class="n">t</span><span class="p">)</span> <span class="o">++</span> <span class="n">wordify</span> <span class="n">x</span></div><div class='line' id='LC38'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">splitter</span> <span class="n">div</span> <span class="n">suffix</span> <span class="ow">=</span> <span class="kr">let</span> <span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span> <span class="ow">=</span> <span class="n">n</span> <span class="p">`</span><span class="n">divMod</span><span class="p">`</span> <span class="n">div</span></div><div class='line' id='LC39'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">in</span> <span class="p">(</span><span class="n">wordify</span> <span class="n">t</span><span class="p">)</span> <span class="o">++</span> <span class="n">suffix</span> <span class="o">++</span> <span class="n">wordify</span> <span class="n">x</span></div><div class='line' id='LC40'>&nbsp;</div><div class='line' id='LC41'><span class="c1">-- Optimized version of length (wordify n)</span></div><div class='line' id='LC42'><span class="c1">-- Used in number crunching</span></div><div class='line' id='LC43'><span class="nf">wordLength</span> <span class="n">n</span> <span class="ow">=</span> <span class="n">wordLength&#39;</span> <span class="mi">0</span> <span class="n">n</span></div><div class='line' id='LC44'>&nbsp;</div><div class='line' id='LC45'><span class="c1">-- Tail recursive version</span></div><div class='line' id='LC46'><span class="nf">wordLength&#39;</span> <span class="ow">::</span> <span class="kt">Int64</span> <span class="ow">-&gt;</span> <span class="kt">Int64</span> <span class="ow">-&gt;</span> <span class="kt">Int64</span></div><div class='line' id='LC47'><span class="nf">wordLength&#39;</span> <span class="o">!</span><span class="n">pad</span> <span class="o">!</span><span class="n">n</span></div><div class='line' id='LC48'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">10</span>         <span class="ow">=</span> <span class="n">lenOnes</span> <span class="o">!</span> <span class="n">n</span> <span class="o">+</span> <span class="n">pad</span></div><div class='line' id='LC49'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">20</span>         <span class="ow">=</span> <span class="n">lenTeens</span> <span class="o">!</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">10</span><span class="p">)</span> <span class="o">+</span> <span class="n">pad</span></div><div class='line' id='LC50'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">100</span>        <span class="ow">=</span> <span class="n">splitterTen</span></div><div class='line' id='LC51'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1000</span>       <span class="ow">=</span> <span class="n">splitter</span> <span class="mi">100</span> <span class="mi">7</span></div><div class='line' id='LC52'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1000000</span>    <span class="ow">=</span> <span class="n">splitter</span> <span class="mi">1000</span> <span class="mi">8</span></div><div class='line' id='LC53'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">otherwise</span>      <span class="ow">=</span> <span class="n">splitter</span> <span class="mi">1000000</span> <span class="mi">7</span></div><div class='line' id='LC54'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">where</span></div><div class='line' id='LC55'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">splitterTen</span> <span class="ow">=</span> <span class="kr">let</span> <span class="o">!</span><span class="p">(</span><span class="o">!</span><span class="n">t</span><span class="p">,</span> <span class="o">!</span><span class="n">x</span><span class="p">)</span> <span class="ow">=</span>  <span class="n">n</span> <span class="p">`</span><span class="n">divMod</span><span class="p">`</span> <span class="mi">10</span></div><div class='line' id='LC56'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">in</span> <span class="n">wordLength&#39;</span> <span class="p">(</span><span class="n">lenTens</span> <span class="o">!</span> <span class="n">t</span> <span class="o">+</span> <span class="n">pad</span><span class="p">)</span> <span class="n">x</span></div><div class='line' id='LC57'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">splitter</span> <span class="o">!</span><span class="n">d</span> <span class="o">!</span><span class="n">suffix</span> <span class="ow">=</span> <span class="kr">let</span> <span class="o">!</span><span class="p">(</span><span class="o">!</span><span class="n">t</span><span class="p">,</span> <span class="o">!</span><span class="n">x</span><span class="p">)</span> <span class="ow">=</span> <span class="n">n</span> <span class="p">`</span><span class="n">divMod</span><span class="p">`</span> <span class="n">d</span></div><div class='line' id='LC58'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">in</span> <span class="n">wordLength&#39;</span> <span class="p">(</span><span class="n">wordLength&#39;</span> <span class="p">(</span><span class="n">suffix</span><span class="o">+</span><span class="n">pad</span><span class="p">)</span> <span class="n">t</span><span class="p">)</span> <span class="n">x</span></div><div class='line' id='LC59'>&nbsp;</div><div class='line' id='LC60'><span class="c1">-- Tail recursive</span></div><div class='line' id='LC61'><span class="nf">solve</span> <span class="ow">::</span> <span class="kt">Int64</span> <span class="ow">-&gt;</span> <span class="p">(</span><span class="kt">Int64</span><span class="p">,</span> <span class="kt">Int64</span><span class="p">,</span> <span class="kt">Int64</span><span class="p">)</span> <span class="ow">-&gt;</span> <span class="p">[</span><span class="kt">Int64</span><span class="p">]</span> <span class="ow">-&gt;</span> <span class="p">(</span><span class="kt">Int64</span><span class="p">,</span> <span class="kt">Int64</span><span class="p">,</span> <span class="kt">Int64</span><span class="p">)</span></div><div class='line' id='LC62'><span class="nf">solve</span> <span class="o">!</span><span class="n">n</span> <span class="o">!</span><span class="n">acc</span><span class="o">@</span><span class="p">(</span><span class="o">!</span><span class="n">sumNum</span><span class="p">,</span> <span class="o">!</span><span class="n">sumLen</span><span class="p">,</span> <span class="o">!</span><span class="n">curr</span><span class="p">)</span> <span class="p">(</span><span class="o">!</span><span class="n">num</span><span class="kt">:</span><span class="n">nums</span><span class="p">)</span></div><div class='line' id='LC63'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">sumLen&#39;</span> <span class="o">&gt;=</span> <span class="n">n</span> <span class="ow">=</span> <span class="p">(</span><span class="n">sumNum&#39;</span><span class="p">,</span> <span class="n">sumLen</span><span class="p">,</span> <span class="n">num</span><span class="p">)</span></div><div class='line' id='LC64'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">otherwise</span> <span class="ow">=</span> <span class="n">solve</span> <span class="n">n</span> <span class="p">(</span><span class="n">sumNum&#39;</span><span class="p">,</span> <span class="n">sumLen&#39;</span><span class="p">,</span> <span class="n">num</span><span class="p">)</span> <span class="n">nums</span></div><div class='line' id='LC65'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">where</span></div><div class='line' id='LC66'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">sumNum&#39;</span> <span class="ow">=</span> <span class="n">sumNum</span> <span class="o">+</span> <span class="n">num</span></div><div class='line' id='LC67'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">sumLen&#39;</span> <span class="ow">=</span> <span class="n">sumLen</span> <span class="o">+</span> <span class="n">wordLength</span> <span class="n">num</span></div><div class='line' id='LC68'>&nbsp;</div><div class='line' id='LC69'><span class="nf">solution</span> <span class="ow">::</span> <span class="kt">Int64</span> <span class="ow">-&gt;</span> <span class="p">(</span><span class="kt">Int64</span><span class="p">,</span> <span class="kt">Char</span><span class="p">)</span></div><div class='line' id='LC70'><span class="nf">solution</span> <span class="o">!</span><span class="n">x</span> <span class="ow">=</span></div><div class='line' id='LC71'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">let</span> <span class="p">(</span><span class="n">sumNum</span><span class="p">,</span> <span class="n">sumLen</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="ow">=</span> <span class="n">solve</span> <span class="n">x</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span> <span class="p">[</span><span class="mi">1</span><span class="o">..</span><span class="p">]</span></div><div class='line' id='LC72'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">in</span> <span class="p">(</span><span class="n">sumNum</span><span class="p">,</span> <span class="p">(</span><span class="n">wordify</span> <span class="n">n</span><span class="p">)</span> <span class="o">!!</span> <span class="p">(</span><span class="n">fromIntegral</span> <span class="o">$</span> <span class="n">x</span> <span class="o">-</span> <span class="n">sumLen</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span></div><div class='line' id='LC73'>&nbsp;</div><div class='line' id='LC74'><span class="nf">main</span> <span class="ow">=</span> <span class="kr">do</span></div><div class='line' id='LC75'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">print</span> <span class="o">$</span> <span class="n">solution</span> <span class="mi">1234</span> <span class="c1">-- Make sure we are sane</span></div><div class='line' id='LC76'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">print</span> <span class="o">$</span> <span class="n">solution</span> <span class="mi">51000000000</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1134262/52e45b569d10ac2f6bdb280f4ec3de326fd0df61/WordNumberSlow.hs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1134262#file_word_number_slow.hs" style="float:right;margin-right:10px;color:#666">WordNumberSlow.hs</a>
            <a href="https://gist.github.com/1134262">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>


<p>OK, I lied. It was not my first attempt. My initial version had space leaks which led to the program eating up 1GB RAM in several seconds. Using <a href="http://book.realworldhaskell.org/read/profiling-and-optimization.html">profiling techniques from Real World Haskell</a>, I identified the space leaks and sprinkled Bang patterns everywhere.</p>

<p>So how well does that run?</p>

<p>Well, I don&#8217;t know.</p>

<p>As far as I remember, one version finished in <strong>12 minutes</strong>, but I am not sure it is the version posted here. In short, the performance was not bearable.</p>

<p>Now if we were using Perl or Ruby, that might not have been too surprising. But for Haskell this seems something obvious is missing.</p>

<h2>The solution</h2>

<p>So there I went, I asked for help on <a href="http://stackoverflow.com/questions/6970904/analyzing-slow-performance-of-a-haskell-program">StackOverflow</a> and in <a href="http://www.haskell.org/pipermail/haskell-cafe/2011-August/094464.html">Haskell-cafe</a>.</p>

<p>There I got help from Bryan O&#8217;Sullivan who posted <a href="https://gist.github.com/1133048#file_wordy.hs">his version</a>. <a href="http://www.haskell.org/pipermail/haskell-cafe/2011-August/094499.html">Reiner Pope said</a> that with <code>-fllvm</code>, it finishes in about <strong>1.5 minutes</strong>. (OK, he did not exactly say that. He said Haskell version is half as fast as C++.)</p>

<h2>But it did not work for me</h2>

<p>So I downloaded Bryan&#8217;s code (we&#8217;ll call it <code>RealWordNumber.hs</code> from now on, sorry bad pun <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  and off I went in excitement:</p>

<pre><code>&gt; ghc -O2 RealWordNumber.hs
&gt; RealWordNumber
&gt; Sum: 1
&gt; Segmentation fault/access violation in generated code
</code></pre>

<p>Since <code>RealWordNumber.hs</code> uses <code>Int</code>, there is an integer overflow in his code. That&#8217;s also exactly the reason why I had to use <code>Int64</code> in my original version.</p>

<p>(I was using Windows where GHC only has 32-bit)</p>

<p>So I patched his code to use <code>Int64</code> and off I went. It never finished, I had to kill it.</p>

<p>Huh?</p>

<h2>The culprit &#8212; 64-bit integers are slow in 32-bit programs</h2>

<p>OK the <strong>major</strong> reason turned out to be this. To confirm, I compiled the C++ version to 32-bit using VC++ (<code>cl /Ox WordNumber.cpp</code>). It took <strong>4 minutes</strong> to complete.</p>

<p>So I took <code>RealWordNumber.hs</code> to Arch Linux x64. It turned out that <code>Int</code> in GHC x64 is actually <code>Int64</code>, and the program completed in <strong>5 minutes</strong> (<code>ghc -O2</code>).</p>

<p>Since the <a href="http://hackage.haskell.org/trac/ghc/ticket/5103">LLVM backend for GHC 7.0.3 seemed broken</a> at the time I write this, and I did not want to go fixing it immediately, I trusted that it would really turn to <strong>1.5 minutes</strong> if I ran it with <code>-fllvm</code>.</p>

<h2>Now the fun part &#8212; breaking down performance improvements</h2>

<p>&#8220;So,&#8221; I figured, &#8220;I just needed to take my original version and put it on Arch x64&#8243;. So I tried that, and it was still taking longer than my patience allowed (8+ minutes). The following will explain how I got my version up to the same league with <code>RealWordNumber.hs</code>, step by step.</p>

<p>First of all, I tuned the number down to 510 million (510000000) to avoid having to wait all day doing this.</p>

<p>Here is a base line using 510 million as an input:</p>

<pre><code>RealWordNumber -- 1.94 seconds
K2WordNumber   -- 5.41 seconds
</code></pre>

<p>From here we see that if I had waited about 15 minutes, I could have seen my original version finish <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>

<p>Now on to how I got it up to speed:</p>

<h3>1. Change <code>divMod</code> to <code>quotRem</code></h3>

<p>I think I heard it from #haskell, people say that <code>quotRem</code> is faster than <code>divMod</code>. So I simply changed all <code>divMod</code> to <code>quotRem</code>. Look at what we&#8217;ve got:</p>

<pre><code>K2WordNumber-1 -- 4.48 seconds
</code></pre>

<p>OK, that was true. A surprising huge speed up.</p>

<h3>2. Change <code>Int64</code> to <code>Int</code></h3>

<p>Since GHC x64&#8242;s <code>Int</code> is actually 64-bit, I changed all <code>Int64</code> to <code>Int</code> and removed the unnecessary <code>fromIntegral</code> calls.</p>

<pre><code>K2WordNumber-2 -- 3.20 seconds
</code></pre>

<p>2 simple changes, we are already getting there <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>

<h3>3. Change <code>quotRem</code> to separate <code>quot</code> and <code>rem</code></h3>

<p>Since <code>quotRem</code> returns a tuple of boxed <code>Int64</code>, which may need to be constructed and reconstructed. <a href="http://www.haskell.org/pipermail/haskell-cafe/2011-August/094466.html">Max Bolingbroke suggested</a> that using <code>quot</code> and <code>rem</code> separately might help. Here&#8217;s the experiment result</p>

<pre><code>K2WordNumber-3 -- 3.13 seconds
</code></pre>

<p>It might have been an improvement, but might have just been experimental error. Unfortunately it seems like not much speed up was achieved.</p>

<h3>4. Removed redundant parameters</h3>

<p>It turned out in my original version, there were several redundant parameters being passed <em>recursively</em> in the function <code>solve</code> (the <code>acc</code> label, and <code>curr</code>). I used <code>ghc -Wall</code> to track them and removed them.</p>

<pre><code>K2WordNumber-4 -- 3.16 seconds
</code></pre>

<p>No change. Since GHC figured out enough to warn me, it probably did the right thing and optimized them for me anyway.</p>

<h3>5. Simplified parameter passing</h3>

<p>The original <code>solve</code> function looked like this</p>

<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">solve <span style="color: #339933; font-weight: bold;">::</span> Int64 <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Int64<span style="color: #339933; font-weight: bold;">,</span> Int64<span style="color: #339933; font-weight: bold;">,</span> Int64<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>Int64<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Int64<span style="color: #339933; font-weight: bold;">,</span> Int64<span style="color: #339933; font-weight: bold;">,</span> Int64<span style="color: green;">&#41;</span><br />
solve <span style="color: #339933; font-weight: bold;">!</span>n <span style="color: #339933; font-weight: bold;">!</span>acc<span style="color: #339933; font-weight: bold;">@</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">!</span>sumNum<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #339933; font-weight: bold;">!</span>sumLen<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #339933; font-weight: bold;">!</span>curr<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">!</span>num:nums<span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> sumLen' <span style="color: #339933; font-weight: bold;">&gt;=</span> n <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>sumNum'<span style="color: #339933; font-weight: bold;">,</span> sumLen<span style="color: #339933; font-weight: bold;">,</span> num<span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:otherwise"><span style="font-weight: bold;">otherwise</span></a> <span style="color: #339933; font-weight: bold;">=</span> solve n <span style="color: green;">&#40;</span>sumNum'<span style="color: #339933; font-weight: bold;">,</span> sumLen'<span style="color: #339933; font-weight: bold;">,</span> num<span style="color: green;">&#41;</span> nums<br />
&nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">where</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; sumNum' <span style="color: #339933; font-weight: bold;">=</span> sumNum <span style="color: #339933; font-weight: bold;">+</span> num<br />
&nbsp; &nbsp; &nbsp; &nbsp; sumLen' <span style="color: #339933; font-weight: bold;">=</span> sumLen <span style="color: #339933; font-weight: bold;">+</span> wordLength num</div></td></tr></tbody></table></div>

<p>To quote Bryan:</p>

<blockquote>
  <p>You&#8217;re passing parameters in three different ways: a regular Int, a 3-tuple, and a list! Whoa.</p>
</blockquote>

<p>He is right. I also noticed that the <code>n</code> parameter does not change and does not need to be passed on. <a href="http://blog.johantibell.com/2010/09/static-argument-transformation.html">Johan Tibell</a> blogged that GHC could in theory create this closure for us. But I did it manually to see what happens:</p>

<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">solve <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a><span style="color: #339933; font-weight: bold;">,</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a><span style="color: #339933; font-weight: bold;">,</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a><span style="color: green;">&#41;</span><br />
solve n <span style="color: #339933; font-weight: bold;">=</span> go <span style="color: red;">0</span> <span style="color: red;">0</span> <span style="color: red;">1</span><br />
&nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">where</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; go <span style="color: #339933; font-weight: bold;">!</span>sumNum sumLen i<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> sumLen' <span style="color: #339933; font-weight: bold;">&gt;=</span> n <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>sumNum'<span style="color: #339933; font-weight: bold;">,</span> sumLen<span style="color: #339933; font-weight: bold;">,</span> i<span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:otherwise"><span style="font-weight: bold;">otherwise</span></a> <span style="color: #339933; font-weight: bold;">=</span> go sumNum' sumLen' <span style="color: green;">&#40;</span>i<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">where</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sumNum' <span style="color: #339933; font-weight: bold;">=</span> sumNum <span style="color: #339933; font-weight: bold;">+</span> i<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sumLen' <span style="color: #339933; font-weight: bold;">=</span> sumLen <span style="color: #339933; font-weight: bold;">+</span> wordLength i</div></td></tr></tbody></table></div>

<p>That resulted in</p>

<pre><code>K2WordNumber-5 -- 2.87 seconds
</code></pre>

<p>Good.</p>

<h3>6. Using unboxed integer types</h3>

<p>I replaced <code>quot</code> and <code>rem</code> with these</p>

<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: green;">&#40;</span>I# a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">//</span> <span style="color: green;">&#40;</span>I# b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> I# <span style="color: green;">&#40;</span>a `quotInt#` b<span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: green;">&#40;</span>I# a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">%</span> <span style="color: green;">&#40;</span>I# b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> I# <span style="color: green;">&#40;</span>a `remInt#` b<span style="color: green;">&#41;</span></div></td></tr></tbody></table></div>

<p>K2WordNumber-6 &#8212; 2.77 seconds</p>

<p>Not as much as I had thought</p>

<h3>7. Using <code>Data.Vector.Unboxed</code> instead of <code>Data.Array.Unboxed</code></h3>

<pre><code>K2WordNumber-7 -- 2.50 seconds
</code></pre>

<h3>8. Using <code>Data.Vector.Generic.unsafeIndex</code> instead of the <code>(!)</code> operator</h3>

<pre><code>K2WordNumber-8 -- 2.20 seconds
</code></pre>

<p>Wow, this one was huge.</p>

<h3>9. Move the length vectors inside <code>wordLength</code>&#8216;s closure</h3>

<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">wordLength <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a><br />
wordLength i <span style="color: #339933; font-weight: bold;">=</span> go <span style="color: red;">0</span> i<br />
&nbsp; &nbsp; <span style="color: #06c; font-weight: bold;">where</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; go <span style="color: #339933; font-weight: bold;">!</span>pad <span style="color: #339933; font-weight: bold;">!</span>n<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> n <span style="color: #339933; font-weight: bold;">&lt;</span> <span style="color: red;">10</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">=</span> lenOnes `VG<span style="color: #339933; font-weight: bold;">.</span>unsafeIndex` n <span style="color: #339933; font-weight: bold;">+</span> pad<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> n <span style="color: #339933; font-weight: bold;">&lt;</span> <span style="color: red;">20</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">=</span> lenTeens `VG<span style="color: #339933; font-weight: bold;">.</span>unsafeIndex` <span style="color: green;">&#40;</span>n<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">10</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">+</span> pad<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> n <span style="color: #339933; font-weight: bold;">&lt;</span> <span style="color: red;">100</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #339933; font-weight: bold;">=</span> go <span style="color: green;">&#40;</span>lenTens `VG<span style="color: #339933; font-weight: bold;">.</span>unsafeIndex` <span style="color: green;">&#40;</span>n<span style="color: #339933; font-weight: bold;">//</span><span style="color: red;">10</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">+</span> pad<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>n<span style="color: #339933; font-weight: bold;">%</span>10<span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> n <span style="color: #339933; font-weight: bold;">&lt;</span> <span style="color: red;">1000</span> &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">=</span> go <span style="color: green;">&#40;</span>go <span style="color: green;">&#40;</span><span style="color: red;">7</span><span style="color: #339933; font-weight: bold;">+</span>pad<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>n<span style="color: #339933; font-weight: bold;">//</span><span style="color: red;">100</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>n<span style="color: #339933; font-weight: bold;">%</span>100<span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> n <span style="color: #339933; font-weight: bold;">&lt;</span> <span style="color: red;">1000000</span> &nbsp; &nbsp;<span style="color: #339933; font-weight: bold;">=</span> go <span style="color: green;">&#40;</span>go <span style="color: green;">&#40;</span><span style="color: red;">8</span><span style="color: #339933; font-weight: bold;">+</span>pad<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>n<span style="color: #339933; font-weight: bold;">//</span><span style="color: red;">1000</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>n<span style="color: #339933; font-weight: bold;">%</span>1000<span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933; font-weight: bold;">|</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:otherwise"><span style="font-weight: bold;">otherwise</span></a> &nbsp; &nbsp; &nbsp;<span style="color: #339933; font-weight: bold;">=</span> go <span style="color: green;">&#40;</span>go <span style="color: green;">&#40;</span><span style="color: red;">7</span><span style="color: #339933; font-weight: bold;">+</span>pad<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>n<span style="color: #339933; font-weight: bold;">//</span><span style="color: red;">1000000</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>n<span style="color: #339933; font-weight: bold;">%</span>1000000<span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: green;">&#40;</span>I# a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">//</span> <span style="color: green;">&#40;</span>I# b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> I# <span style="color: green;">&#40;</span>a `quotInt#` b<span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: green;">&#40;</span>I# a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">%</span> <span style="color: green;">&#40;</span>I# b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> I# <span style="color: green;">&#40;</span>a `remInt#` b<span style="color: green;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; lenOnes <span style="color: #339933; font-weight: bold;">=</span> VU<span style="color: #339933; font-weight: bold;">.</span>fromList <span style="color: green;">&#91;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">4</span><span style="color: green;">&#93;</span> <span style="color: #5d478b; font-style: italic;">-- &quot;&quot;, &quot;one&quot;,&quot;two&quot;, ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; lenTens <span style="color: #339933; font-weight: bold;">=</span> VU<span style="color: #339933; font-weight: bold;">.</span>fromList <span style="color: green;">&#91;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">7</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: green;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; lenTeens <span style="color: #339933; font-weight: bold;">=</span> VU<span style="color: #339933; font-weight: bold;">.</span>fromList <span style="color: green;">&#91;</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">8</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">8</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">7</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">7</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">9</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">8</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">8</span><span style="color: green;">&#93;</span> <span style="color: #5d478b; font-style: italic;">-- first element is &quot;ten&quot; 3</span></div></td></tr></tbody></table></div>

<p>K2WordNumber-9a &#8212; 2.20 seconds</p>

<p>No improvement at all&#8230; But if I add bangs before the vectors&#8230;</p>

<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933; font-weight: bold;">!</span>lenOnes <span style="color: #339933; font-weight: bold;">=</span> VU<span style="color: #339933; font-weight: bold;">.</span>fromList <span style="color: green;">&#91;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">4</span><span style="color: green;">&#93;</span> <span style="color: #5d478b; font-style: italic;">-- &quot;&quot;, &quot;one&quot;,&quot;two&quot;, ...</span><br />
<span style="color: #339933; font-weight: bold;">!</span>lenTens <span style="color: #339933; font-weight: bold;">=</span> VU<span style="color: #339933; font-weight: bold;">.</span>fromList <span style="color: green;">&#91;</span><span style="color: red;">0</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">5</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">7</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: green;">&#93;</span><br />
<span style="color: #339933; font-weight: bold;">!</span>lenTeens <span style="color: #339933; font-weight: bold;">=</span> VU<span style="color: #339933; font-weight: bold;">.</span>fromList <span style="color: green;">&#91;</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">8</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">8</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">7</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">7</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">9</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">8</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">8</span><span style="color: green;">&#93;</span> <span style="color: #5d478b; font-style: italic;">-- first element is &quot;ten&quot; 3</span></div></td></tr></tbody></table></div>

<p>K2WordNumber-9 &#8212; 2.00 seconds</p>

<p><strong>WOW!!</strong> That was a large speed up. It turns out the reason for the speed up is related to strictness. <code>wordLength</code> is not strict in all <code>lenOnes</code>, <code>lenTens</code> and <code>lenTeens</code>, so they might be garbaged collected (?). Putting bangs on them allow them to stay alive. Since we need to use those arrays very frequently, it was better for them to be around all the time.</p>

<p>Haskell did not allow putting bangs on &#8220;global&#8221; variables, so I missed it in my original bang sprinkling exercise.</p>

<p>OK, so now we have transformed my original sloppy Haskell program to a &#8220;well-performing&#8221; Haskell program. Since it performs the same as Bryan O&#8217;Sullivan (casually) tuned version, I am going to assume it&#8217;s probably as fast as it should (before maybe resorting to serious hacks).</p>

<p>And here is the final version of the code:</p>

<div id="gist-1134262" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c1">-- Problem: Find the 51000000000-th character of the string (wordNumber Infinity)</span></div><div class='line' id='LC2'><span class="c1">-- where a wordNumber is defined as</span></div><div class='line' id='LC3'><span class="c1">--</span></div><div class='line' id='LC4'><span class="c1">-- wordNumber 1 = &quot;one&quot;</span></div><div class='line' id='LC5'><span class="c1">-- wordNumber 2 = &quot;onetwo&quot;</span></div><div class='line' id='LC6'><span class="c1">-- wordNumber 3 = &quot;onetwothree&quot;</span></div><div class='line' id='LC7'><span class="c1">-- wordNumber 15 = &quot;onetwothreefourfivesixseveneightnineteneleventwelvethirteenfourteenfifteen&quot;</span></div><div class='line' id='LC8'><span class="c1">-- ...</span></div><div class='line' id='LC9'><span class="c1">--</span></div><div class='line' id='LC10'><span class="c1">-- The answer should be presented as ( sum of all numbers up to that point</span></div><div class='line' id='LC11'><span class="c1">--                                   , the 51000000000-th character</span></div><div class='line' id='LC12'><span class="c1">--                                   )</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'><span class="c1">-- This is a Haskell performance tuning exercise, trying to achieve C like performance.</span></div><div class='line' id='LC15'><span class="c1">-- Full guided story can be found at</span></div><div class='line' id='LC16'><span class="c1">-- http://cfc.kizzx2.com/index.php/in-search-of-performance-in-haskell/</span></div><div class='line' id='LC17'><span class="c1">--</span></div><div class='line' id='LC18'><span class="c1">-- (This Word Number problem is actually a misunderstood version of ITA&#39;s version)</span></div><div class='line' id='LC19'><br/></div><div class='line' id='LC20'><span class="cm">{-# LANGUAGE BangPatterns, MagicHash #-}</span></div><div class='line' id='LC21'><br/></div><div class='line' id='LC22'><span class="kr">import</span> <span class="k">qualified</span> <span class="nn">Data.Vector.Unboxed</span> <span class="k">as</span> <span class="n">VU</span></div><div class='line' id='LC23'><span class="kr">import</span> <span class="nn">Data.Vector.Unboxed</span> <span class="p">((</span><span class="o">!</span><span class="p">))</span></div><div class='line' id='LC24'><span class="kr">import</span> <span class="k">qualified</span> <span class="nn">Data.Vector.Generic</span> <span class="k">as</span> <span class="n">VG</span></div><div class='line' id='LC25'><span class="kr">import</span> <span class="nn">GHC.Base</span> <span class="p">(</span><span class="kt">Int</span><span class="p">(</span><span class="o">..</span><span class="p">),</span> <span class="nf">quotInt</span><span class="o">#</span><span class="p">,</span> <span class="nf">remInt</span><span class="o">#</span><span class="p">)</span></div><div class='line' id='LC26'><br/></div><div class='line' id='LC27'><span class="nf">ones</span><span class="p">,</span> <span class="n">tens</span><span class="p">,</span> <span class="n">teens</span> <span class="ow">::</span> <span class="p">[</span><span class="kt">String</span><span class="p">]</span></div><div class='line' id='LC28'><span class="nf">ones</span> <span class="ow">=</span> <span class="p">[</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="s">&quot;one&quot;</span><span class="p">,</span> <span class="s">&quot;two&quot;</span><span class="p">,</span> <span class="s">&quot;three&quot;</span><span class="p">,</span> <span class="s">&quot;four&quot;</span><span class="p">,</span> <span class="s">&quot;five&quot;</span><span class="p">,</span> <span class="s">&quot;six&quot;</span><span class="p">,</span> <span class="s">&quot;seven&quot;</span><span class="p">,</span> <span class="s">&quot;eight&quot;</span><span class="p">,</span> <span class="s">&quot;nine&quot;</span><span class="p">]</span></div><div class='line' id='LC29'><span class="nf">tens</span> <span class="ow">=</span> <span class="p">[</span><span class="s">&quot;&quot;</span><span class="p">,</span> <span class="s">&quot;ten&quot;</span><span class="p">,</span> <span class="s">&quot;twenty&quot;</span><span class="p">,</span> <span class="s">&quot;thirty&quot;</span><span class="p">,</span> <span class="s">&quot;forty&quot;</span><span class="p">,</span> <span class="s">&quot;fifty&quot;</span><span class="p">,</span> <span class="s">&quot;sixty&quot;</span><span class="p">,</span> <span class="s">&quot;seventy&quot;</span><span class="p">,</span> <span class="s">&quot;eighty&quot;</span><span class="p">,</span> <span class="s">&quot;ninety&quot;</span><span class="p">]</span></div><div class='line' id='LC30'><span class="nf">teens</span> <span class="ow">=</span> <span class="p">[</span><span class="s">&quot;ten&quot;</span><span class="p">,</span> <span class="s">&quot;eleven&quot;</span><span class="p">,</span> <span class="s">&quot;twelve&quot;</span><span class="p">,</span> <span class="s">&quot;thirteen&quot;</span><span class="p">,</span> <span class="s">&quot;fourteen&quot;</span><span class="p">,</span> <span class="s">&quot;fifteen&quot;</span><span class="p">,</span> <span class="s">&quot;sixteen&quot;</span><span class="p">,</span> <span class="s">&quot;seventeen&quot;</span><span class="p">,</span> <span class="s">&quot;eighteen&quot;</span><span class="p">,</span> <span class="s">&quot;nineteen&quot;</span><span class="p">]</span></div><div class='line' id='LC31'><br/></div><div class='line' id='LC32'><span class="nf">wordify</span> <span class="ow">::</span> <span class="kt">Int</span> <span class="ow">-&gt;</span> <span class="kt">String</span></div><div class='line' id='LC33'><span class="nf">wordify</span> <span class="n">n</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">10</span>         <span class="ow">=</span> <span class="n">ones</span> <span class="o">!!</span> <span class="n">n</span></div><div class='line' id='LC35'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">20</span>         <span class="ow">=</span> <span class="n">teens</span> <span class="o">!!</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">10</span><span class="p">)</span></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">100</span>        <span class="ow">=</span> <span class="n">splitterTen</span></div><div class='line' id='LC37'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1000</span>       <span class="ow">=</span> <span class="n">splitter</span> <span class="mi">100</span> <span class="s">&quot;hundred&quot;</span></div><div class='line' id='LC38'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1000000</span>    <span class="ow">=</span> <span class="n">splitter</span> <span class="mi">1000</span> <span class="s">&quot;thousand&quot;</span></div><div class='line' id='LC39'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">otherwise</span>      <span class="ow">=</span> <span class="n">splitter</span> <span class="mi">1000000</span> <span class="s">&quot;million&quot;</span></div><div class='line' id='LC40'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">where</span></div><div class='line' id='LC41'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">splitterTen</span> <span class="ow">=</span> <span class="kr">let</span> <span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span> <span class="ow">=</span> <span class="n">n</span> <span class="p">`</span><span class="n">quotRem</span><span class="p">`</span> <span class="mi">10</span></div><div class='line' id='LC42'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">in</span> <span class="p">(</span><span class="n">tens</span> <span class="o">!!</span> <span class="n">t</span><span class="p">)</span> <span class="o">++</span> <span class="n">wordify</span> <span class="n">x</span></div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">splitter</span> <span class="n">d</span> <span class="n">suffix</span> <span class="ow">=</span> <span class="kr">let</span> <span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span> <span class="ow">=</span> <span class="n">n</span> <span class="p">`</span><span class="n">quotRem</span><span class="p">`</span> <span class="n">d</span></div><div class='line' id='LC44'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">in</span> <span class="p">(</span><span class="n">wordify</span> <span class="n">t</span><span class="p">)</span> <span class="o">++</span> <span class="n">suffix</span> <span class="o">++</span> <span class="n">wordify</span> <span class="n">x</span></div><div class='line' id='LC45'><br/></div><div class='line' id='LC46'><span class="c1">-- Tail recursive version</span></div><div class='line' id='LC47'><span class="nf">wordLength</span> <span class="ow">::</span> <span class="kt">Int</span> <span class="ow">-&gt;</span> <span class="kt">Int</span></div><div class='line' id='LC48'><span class="nf">wordLength</span> <span class="n">i</span> <span class="ow">=</span> <span class="n">go</span> <span class="mi">0</span> <span class="n">i</span></div><div class='line' id='LC49'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">where</span></div><div class='line' id='LC50'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">go</span> <span class="o">!</span><span class="n">pad</span> <span class="o">!</span><span class="n">n</span></div><div class='line' id='LC51'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">10</span>         <span class="ow">=</span> <span class="n">lenOnes</span> <span class="p">`</span><span class="kt">VG</span><span class="o">.</span><span class="n">unsafeIndex</span><span class="p">`</span> <span class="n">n</span> <span class="o">+</span> <span class="n">pad</span></div><div class='line' id='LC52'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">20</span>         <span class="ow">=</span> <span class="n">lenTeens</span> <span class="p">`</span><span class="kt">VG</span><span class="o">.</span><span class="n">unsafeIndex</span><span class="p">`</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">10</span><span class="p">)</span> <span class="o">+</span> <span class="n">pad</span></div><div class='line' id='LC53'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">100</span>        <span class="ow">=</span> <span class="n">go</span> <span class="p">(</span><span class="n">lenTens</span> <span class="p">`</span><span class="kt">VG</span><span class="o">.</span><span class="n">unsafeIndex</span><span class="p">`</span> <span class="p">(</span><span class="n">n</span><span class="o">//</span><span class="mi">10</span><span class="p">)</span> <span class="o">+</span> <span class="n">pad</span><span class="p">)</span> <span class="p">(</span><span class="n">n</span><span class="o">%</span><span class="mi">10</span><span class="p">)</span></div><div class='line' id='LC54'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1000</span>       <span class="ow">=</span> <span class="n">go</span> <span class="p">(</span><span class="n">go</span> <span class="p">(</span><span class="mi">7</span><span class="o">+</span><span class="n">pad</span><span class="p">)</span> <span class="p">(</span><span class="n">n</span><span class="o">//</span><span class="mi">100</span><span class="p">))</span> <span class="p">(</span><span class="n">n</span><span class="o">%</span><span class="mi">100</span><span class="p">)</span></div><div class='line' id='LC55'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">1000000</span>    <span class="ow">=</span> <span class="n">go</span> <span class="p">(</span><span class="n">go</span> <span class="p">(</span><span class="mi">8</span><span class="o">+</span><span class="n">pad</span><span class="p">)</span> <span class="p">(</span><span class="n">n</span><span class="o">//</span><span class="mi">1000</span><span class="p">))</span> <span class="p">(</span><span class="n">n</span><span class="o">%</span><span class="mi">1000</span><span class="p">)</span></div><div class='line' id='LC56'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">otherwise</span>      <span class="ow">=</span> <span class="n">go</span> <span class="p">(</span><span class="n">go</span> <span class="p">(</span><span class="mi">7</span><span class="o">+</span><span class="n">pad</span><span class="p">)</span> <span class="p">(</span><span class="n">n</span><span class="o">//</span><span class="mi">1000000</span><span class="p">))</span> <span class="p">(</span><span class="n">n</span><span class="o">%</span><span class="mi">1000000</span><span class="p">)</span></div><div class='line' id='LC57'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">(</span><span class="kt">I</span><span class="o">#</span> <span class="n">a</span><span class="p">)</span> <span class="o">//</span> <span class="p">(</span><span class="kt">I</span><span class="o">#</span> <span class="n">b</span><span class="p">)</span> <span class="ow">=</span> <span class="kt">I</span><span class="o">#</span> <span class="p">(</span><span class="n">a</span> <span class="p">`</span><span class="n">quotInt</span><span class="o">#</span><span class="p">`</span> <span class="n">b</span><span class="p">)</span></div><div class='line' id='LC58'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">(</span><span class="kt">I</span><span class="o">#</span> <span class="n">a</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="kt">I</span><span class="o">#</span> <span class="n">b</span><span class="p">)</span> <span class="ow">=</span> <span class="kt">I</span><span class="o">#</span> <span class="p">(</span><span class="n">a</span> <span class="p">`</span><span class="n">remInt</span><span class="o">#</span><span class="p">`</span> <span class="n">b</span><span class="p">)</span></div><div class='line' id='LC59'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">!</span><span class="n">lenOnes</span> <span class="ow">=</span> <span class="kt">VU</span><span class="o">.</span><span class="n">fromList</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">4</span><span class="p">]</span> <span class="c1">-- &quot;&quot;, &quot;one&quot;,&quot;two&quot;, ...</span></div><div class='line' id='LC60'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">!</span><span class="n">lenTens</span> <span class="ow">=</span> <span class="kt">VU</span><span class="o">.</span><span class="n">fromList</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">]</span></div><div class='line' id='LC61'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">!</span><span class="n">lenTeens</span> <span class="ow">=</span> <span class="kt">VU</span><span class="o">.</span><span class="n">fromList</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">8</span><span class="p">]</span> <span class="c1">-- first element is &quot;ten&quot; 3</span></div><div class='line' id='LC62'><br/></div><div class='line' id='LC63'><span class="nf">solve</span> <span class="ow">::</span> <span class="kt">Int</span> <span class="ow">-&gt;</span> <span class="p">(</span><span class="kt">Int</span><span class="p">,</span> <span class="kt">Int</span><span class="p">,</span> <span class="kt">Int</span><span class="p">)</span></div><div class='line' id='LC64'><span class="nf">solve</span> <span class="n">n</span> <span class="ow">=</span> <span class="n">go</span> <span class="mi">0</span> <span class="mi">0</span> <span class="mi">1</span></div><div class='line' id='LC65'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">where</span></div><div class='line' id='LC66'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">go</span> <span class="o">!</span><span class="n">sumNum</span> <span class="n">sumLen</span> <span class="n">i</span></div><div class='line' id='LC67'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">sumLen&#39;</span> <span class="o">&gt;=</span> <span class="n">n</span> <span class="ow">=</span> <span class="p">(</span><span class="n">sumNum&#39;</span><span class="p">,</span> <span class="n">sumLen</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span></div><div class='line' id='LC68'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">|</span> <span class="n">otherwise</span> <span class="ow">=</span> <span class="n">go</span> <span class="n">sumNum&#39;</span> <span class="n">sumLen&#39;</span> <span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span></div><div class='line' id='LC69'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">where</span></div><div class='line' id='LC70'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">sumNum&#39;</span> <span class="ow">=</span> <span class="n">sumNum</span> <span class="o">+</span> <span class="n">i</span></div><div class='line' id='LC71'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">sumLen&#39;</span> <span class="ow">=</span> <span class="n">sumLen</span> <span class="o">+</span> <span class="n">wordLength</span> <span class="n">i</span></div><div class='line' id='LC72'><br/></div><div class='line' id='LC73'><br/></div><div class='line' id='LC74'><span class="nf">solution</span> <span class="ow">::</span> <span class="kt">Int</span> <span class="ow">-&gt;</span> <span class="p">(</span><span class="kt">Int</span><span class="p">,</span> <span class="kt">Char</span><span class="p">)</span></div><div class='line' id='LC75'><span class="nf">solution</span> <span class="n">x</span> <span class="ow">=</span></div><div class='line' id='LC76'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">let</span> <span class="p">(</span><span class="n">sumNum</span><span class="p">,</span> <span class="n">sumLen</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="ow">=</span> <span class="n">solve</span> <span class="n">x</span></div><div class='line' id='LC77'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kr">in</span> <span class="p">(</span><span class="n">sumNum</span><span class="p">,</span> <span class="p">(</span><span class="n">wordify</span> <span class="n">n</span><span class="p">)</span> <span class="o">!!</span> <span class="p">(</span><span class="n">x</span> <span class="o">-</span> <span class="n">sumLen</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span></div><div class='line' id='LC78'><br/></div><div class='line' id='LC79'><span class="nf">main</span> <span class="ow">::</span>  <span class="kt">IO</span> <span class="nb">()</span></div><div class='line' id='LC80'><span class="nf">main</span> <span class="ow">=</span> <span class="kr">do</span></div><div class='line' id='LC81'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">print</span> <span class="o">$</span> <span class="n">solution</span> <span class="mi">510000000</span></div><div class='line' id='LC82'><br/></div><div class='line' id='LC83'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1134262/b1602804fb056d312d1347d1f824d25e4f2706a3/K2WordNumber.hs" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1134262#file_k2_word_number.hs" style="float:right;margin-right:10px;color:#666">K2WordNumber.hs</a>
            <a href="https://gist.github.com/1134262">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>


<h2>Moral of the story</h2>

<ul>
<li>Haskell is a very fast functional language. But for tight areas like this, always consider giving plain old C a try &#8212; it may just save you days of profiling.</li>
<li>64-bit operations in 32-bit programs are slow (&#42;)</li>
<li><code>quotRem</code> is faster than <code>divMod</code></li>
<li>Earlier I argued in #haskell that &#8220;if <code>Integer</code> vs <code>Int</code> matters for your performance, your program is probably written wrong&#8221;. Well, there are cases where it matters quite a lot. It turns out conversions are quite costly. (Though I still believe people should use <code>Integer</code> by default)</li>
<li>Use the wrapper-worker pattern liberally.</li>
<li>If you recurse, make sure every variable actually changes in each recursion. (For most cases this will benefit. If you only recurse several times maybe the closure&#8217;s overhead will be larger.)</li>
<li>Don&#8217;t use a list or tuple just because infinite lists look smart.</li>
<li><code>Data.Vector</code> is a better &#8220;general case&#8221; container than <code>Data.Array</code></li>
<li>Try to reduce the scope of &#8220;global&#8221; variables. Put them in a closure so you can <em>bang</em> on it.</li>
</ul>

<p>&#42; This may be actually the suckage of Windows&#8217; &#8220;WOW64&#8243; (32-bit emulation layer). I have not tried to prove this on 32-bit Linux GHC)</p>

<h3>Extra: the fastest indexing data structure &#8212; functions!?</h3>

<p>I heard from #haskell that &#8220;pattern matching in Haskell is less than O(n)&#8221;. When I heard it, I didn&#8217;t really believe it. I have always thought that N pattern matches means translating into N <code>if-then-else</code> statements for the <em>general case</em>. He said something related to <a href="https://secure.wikimedia.org/wikipedia/en/wiki/Church_encoding">Church encoding</a> but I did not see how it could be applied in the general case to avoid N <code>if-then-else</code> statements.</p>

<p>Just for fun, I tried changing the length vectors into functions:</p>

<div class="codecolorer-container haskell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="haskell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">lenOnes <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">0</span><br />
lenOnes <span style="color: red;">1</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">3</span><br />
lenOnes <span style="color: red;">2</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">3</span><br />
lenOnes <span style="color: red;">3</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">5</span><br />
<span style="color: #5d478b; font-style: italic;">-- ...</span><br />
lenTeens <span style="color: red;">9</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">8</span></div></td></tr></tbody></table></div>

<p>K2WordNumber-10 &#8212; 2.00 seconds</p>

<p>Yes, <em>pattern matching is as fast as <code>unsafeIndex</code></em>. <strong>WOW!</strong> wren nt thornton from the comments below give a more thorough explanation on this one. It turns out GHC is really darn smart and can figure out fast code given &#8220;primitive&#8221; definitions.</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/in-search-of-performance-in-haskell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

