<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>IndianGeek</title>
	
	<link>http://www.indiangeek.net</link>
	<description>Perspectives on Computer Science and Software Engineering</description>
	<pubDate>Thu, 20 Nov 2008 15:11:39 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/Indiangeek" type="application/rss+xml" /><item>
		<title>links for 2008-11-20</title>
		<link>http://feeds.feedburner.com/~r/Indiangeek/~3/459651756/</link>
		<comments>http://www.indiangeek.net/2008/11/20/links-for-2008-11-20/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 15:11:39 +0000</pubDate>
		<dc:creator>sijin</dc:creator>
		
		<category><![CDATA[Misc]]></category>

		<guid isPermaLink="false">http://www.indiangeek.net/2008/11/20/links-for-2008-11-20/</guid>
		<description><![CDATA[

CSS Tip #1: Resetting Your Styles with CSS Reset - Six Revisions
(tags: programming reference web webdev)


SQL Injection Cheat Sheet
(tags: programming reference web security sql)


]]></description>
			<content:encoded><![CDATA[<ul class="delicious">
<li>
<div class="delicious-link"><a href="http://sixrevisions.com/css/css-tips/css-tip-1-resetting-your-styles-with-css-reset/">CSS Tip #1: Resetting Your Styles with CSS Reset - Six Revisions</a></div>
<div class="delicious-tags">(tags: <a href="http://delicious.com/sijin/programming">programming</a> <a href="http://delicious.com/sijin/reference">reference</a> <a href="http://delicious.com/sijin/web">web</a> <a href="http://delicious.com/sijin/webdev">webdev</a>)</div>
</li>
<li>
<div class="delicious-link"><a href="http://ferruh.mavituna.com/sql-injection-cheatsheet-oku/">SQL Injection Cheat Sheet</a></div>
<div class="delicious-tags">(tags: <a href="http://delicious.com/sijin/programming">programming</a> <a href="http://delicious.com/sijin/reference">reference</a> <a href="http://delicious.com/sijin/web">web</a> <a href="http://delicious.com/sijin/security">security</a> <a href="http://delicious.com/sijin/sql">sql</a>)</div>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.indiangeek.net/2008/11/20/links-for-2008-11-20/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.indiangeek.net/2008/11/20/links-for-2008-11-20/</feedburner:origLink></item>
		<item>
		<title>Windows Azure Distilled - A Programmer’s view</title>
		<link>http://feeds.feedburner.com/~r/Indiangeek/~3/453324691/</link>
		<comments>http://www.indiangeek.net/2008/11/14/windows-azure-distilled/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 20:20:02 +0000</pubDate>
		<dc:creator>sijin</dc:creator>
		
		<category><![CDATA[.Net]]></category>

		<category><![CDATA[Software Development]]></category>

		<category><![CDATA[azure]]></category>

		<category><![CDATA[cloud computing]]></category>

		<category><![CDATA[microsoft]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://www.indiangeek.net/?p=420</guid>
		<description><![CDATA[
The launch of Windows Azure has coincided with the launch of the entire cloud computing initiative from Microsoft which makes a lot of business sense but it also makes it hard to differntiate the individual parts of the initiative, here&#8217;s my understanding so far.


Windows Azure
This is the base platform that provides a generic cloud computing [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone" title="Azure" src="http://www.microsoft.com/azure/images/servicesPlatform.jpg" alt="" width="490" height="226" /></p>
<p>The launch of Windows Azure has coincided with the launch of the entire cloud computing initiative from Microsoft which makes a lot of business sense but it also makes it hard to differntiate the individual parts of the initiative, here&#8217;s my understanding so far.</p>
<p><span style="color: #551a8b; text-decoration: underline;"><br />
</span></p>
<h2>Windows Azure</h2>
<p>This is the base platform that provides a generic cloud computing platform for developers to host applications on.  Azure basically offers two core services,</p>
<h3>Compute Service</h3>
<p style="padding-left: 30px; ">This is what people would normally associate with cloud computing. You write an app that <a href="http://msdn.microsoft.com/en-us/library/dd179369.aspx" target="_blank">conforms to the cloud hosting requirements</a> and then deploy it to the cloud where it can be managed and scaled as per the load and performance requirements. Currently there are two types of compute services that can be deployed on Azure</p>
<p style="padding-left: 60px; "><strong>Web Role</strong> - This currently is a WebForms ASP.Net application, but looks like support for ASP.Net MVC and other languages are in the works too. This is just like hosting your application with a hosting provider except that you get load balancing, failover etc. for free. One important caveat here is that the load balancing does not support sticky sessions(as of the CTP) which is a good thing but it does require the developer to use the storage service to store session data.</p>
<p style="padding-left: 60px; "><strong>Worker Role</strong> - This is more like a Windows Service that is deployed on the cloud, it can make outgoing connections but incoming connections are disallowed. But again you get the benefits of load balancing and failover.</p>
<p style="padding-left: 30px; ">The compute services are different from other similar cloud computing options available because it does not require the developer to supply a VM image (unlike <a href="http://aws.amazon.com/ec2/" target="_blank">Amazon EC2</a>) of the application environment to host,  instead the applications run in VMs that are built and maintained by the Microsoft Data Centers that host the applications.</p>
<p style="padding-left: 30px; ">This decision definitely has it pros and cons, it&#8217;s good because</p>
<p style="padding-left: 60px;">- No licensing costs for the OS</p>
<p style="padding-left: 60px;">- No need for updating of the VMs as the applications evolve</p>
<p style="padding-left: 60px;">- Simpler deployment strategy</p>
<p style="padding-left: 60px;">- API available for management of service instances and for reporting on application health</p>
<p style="padding-left: 30px; ">on the other hand</p>
<p style="padding-left: 60px;">- it does restrict the flexibility of what software and services can be accessed from the host VM</p>
<p style="padding-left: 30px; ">This diagram from MSDN gives a nice overview</p>
<p style="padding-left: 30px; "><img class="alignnone" title="Azure Compute Services" src="http://i.msdn.microsoft.com/Dd179341.06fd0ae8-9fef-48fd-9cee-69b83fdbbe47(en-us,MSDN.10).gif" alt="" width="943" height="612" /></p>
<p style="padding-left: 30px; ">All though in reality the deployment internally looks like below with IIS7 and Windows Server 2008 VMs.</p>
<p style="padding-left: 30px; "> </p>
<div id="attachment_427" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.indiangeek.net/wp-content/uploads/2008/11/azure-hosting1.png"><img class="size-full wp-image-427" title="azure-hosting1" src="http://www.indiangeek.net/wp-content/uploads/2008/11/azure-hosting1.png" alt="Azure Hosting" width="500" height="330" /></a><p class="wp-caption-text">Azure Hosting</p></div>
<p style="padding-left: 30px; "> </p>
<h3>Storage Service</h3>
<p style="padding-left: 30px; ">This is the second part of the base Azure platform and is the equivalent of Amazon <a href="http://aws.amazon.com/s3/" target="_blank">S3</a>, <a href="http://aws.amazon.com/sqs/" target="_blank">SQS </a>and <a href="http://aws.amazon.com/simpledb/" target="_blank">SimpleDB </a>all rolled under one name. The equivalents in the Storage Service are called,</p>
<p style="padding-left: 60px; "><strong>Blob </strong>- For storing binary data, most commonly file streams.</p>
<p style="padding-left: 60px; "><strong>Queue  </strong>- For reliable persistent messaging. I found it strange that it was lumped with the storage services because the primary purpose of this seems to be inter service communication between the applications running in the cloud. </p>
<p style="padding-left: 60px; "><strong>Table </strong>- For storing tabular data, different from traditional database tables though.</p>
<p style="padding-left: 30px; ">The important thing to remember here is that these services are available to any application that can talk over HTTP so WPF, WinForms, and non-cloud ASP.Net applications can make use of the storage services as well and benefit from having their data stored in an easily scaled environment. The storage services expose a RESTFul API which makes it easy to access these services from any platform that can talk over HTTP but ofcourse if you&#8217;re using .Net you get some builtin support from the framework.</p>
<p style="padding-left: 30px; "> </p>
<div id="attachment_426" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.indiangeek.net/wp-content/uploads/2008/11/azure-storage-services.png"><img class="size-full wp-image-426" title="azure-storage-services" src="http://www.indiangeek.net/wp-content/uploads/2008/11/azure-storage-services.png" alt="Azure Storage Services" width="500" height="325" /></a><p class="wp-caption-text">Azure Storage Services</p></div>
<p> </p>
<p>The developer story for Windows Azure is as good as anything else from Microsoft, the SDK includes tools to simulate both the compute services and the storage services on your development box, further with the VS addin you can deploy your cloud application straight to the Azure hosting platform, talk about ease of deployment. Here&#8217;s a quick overview of the important tools included in the Azure SDK.</p>
<p style="padding-left: 30px; "><strong>Development Fabric</strong> - Simulates the Azure Compute Services (aka Azure Fabric) on your dev machine complete with a load balancer and failover monitoring.</p>
<p style="padding-left: 30px; "><strong>Development Storage</strong> - Simulates the Azure storage services on your dev machine, the CTP implmentation requires SQL Server 2008 Express but you can also work with SQL Server 2008 if you want.</p>
<p style="padding-left: 30px; "><strong>CSPack</strong> - Command line tool that prepares a service for deployment either to the Development Fabric or to the Windows Azure Fabric.</p>
<p style="padding-left: 30px; "><strong>CSRun </strong>- Command line tool that runs a service (generated from CSPack) on the Development Fabric.</p>
<p>So the development cycle for a Azure app consists of developing locally using the Development Fabric and Development Storage and then deploying to the Azure platform when ready.  Deployments would not normally bring down the sites because the instances are load balanced and are not sticky. </p>
<p>The other good thing is that services can be configured manually be editing the service configuration file or programatically by accessing the ServiceHosting API at runtime, so no redeployment is required if only service configurations are changed (which includes things like number of instances to run).</p>
<p>That&#8217;s basically the core Azure platform, the rest of the stack (<a href="http://www.microsoft.com/azure/netservices.mspx" target="_blank">.Net Services</a>, <a href="http://www.microsoft.com/azure/sql.mspx" target="_blank">SQL Data Services</a> and <a href="http://dev.live.com/" target="_blank">Live Services</a>) are cloud applications built by Microsoft on top of the core Azure platform. Again all these applications expose their API in a RESTFul manner as well making them open to both any technology that can talk over HTTP.</p>
<p> </p>
<div id="attachment_430" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.indiangeek.net/wp-content/uploads/2008/11/azure-services-platform.png"><img class="size-full wp-image-430" title="azure-services-platform" src="http://www.indiangeek.net/wp-content/uploads/2008/11/azure-services-platform.png" alt="Azure Services Platform" width="500" height="311" /></a><p class="wp-caption-text">Azure Services Platform</p></div>
<p> </p>
<p>Personally for me the <a href="http://msdn.microsoft.com/en-us/library/dd179367.aspx" target="_blank">Windows Azure SDK section from MSDN </a>and this <a href="http://download.microsoft.com/download/e/4/3/e43bb484-3b52-4fa8-a9f9-ec60a32954bc/Azure_Services_Platform.docx">document from David Chappel</a> were the most useful pieces of documentation to understand the entire stack.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.indiangeek.net/2008/11/14/windows-azure-distilled/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.indiangeek.net/2008/11/14/windows-azure-distilled/</feedburner:origLink></item>
		<item>
		<title>links for 2008-10-31</title>
		<link>http://feeds.feedburner.com/~r/Indiangeek/~3/438116126/</link>
		<comments>http://www.indiangeek.net/2008/10/31/links-for-2008-10-31/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 15:05:43 +0000</pubDate>
		<dc:creator>sijin</dc:creator>
		
		<category><![CDATA[Misc]]></category>

		<guid isPermaLink="false">http://www.indiangeek.net/2008/10/31/links-for-2008-10-31/</guid>
		<description><![CDATA[

Autodesk story
History of Autodesk
(tags: programming software history)


]]></description>
			<content:encoded><![CDATA[<ul class="delicious">
<li>
<div class="delicious-link"><a href="http://www.fourmilab.ch/autofile/www/tableofcontents2_117.html">Autodesk story</a></div>
<div class="delicious-extended">History of Autodesk</div>
<div class="delicious-tags">(tags: <a href="http://delicious.com/sijin/programming">programming</a> <a href="http://delicious.com/sijin/software">software</a> <a href="http://delicious.com/sijin/history">history</a>)</div>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.indiangeek.net/2008/10/31/links-for-2008-10-31/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.indiangeek.net/2008/10/31/links-for-2008-10-31/</feedburner:origLink></item>
		<item>
		<title>Using Windows Azure SDK with SQL Server 2005/2008</title>
		<link>http://feeds.feedburner.com/~r/Indiangeek/~3/436142748/</link>
		<comments>http://www.indiangeek.net/2008/10/29/using-windows-azure-with-sql-server-20052008/#comments</comments>
		<pubDate>Wed, 29 Oct 2008 19:10:29 +0000</pubDate>
		<dc:creator>sijin</dc:creator>
		
		<category><![CDATA[.Net]]></category>

		<category><![CDATA[Software Development]]></category>

		<category><![CDATA[windows azure programming microsoft]]></category>

		<guid isPermaLink="false">http://www.indiangeek.net/?p=415</guid>
		<description><![CDATA[I downloaded the Windows Azure SDK today and was going through the quick starts from MSDN , the quickstarts mention that you need SQL Server Express 2005/2008 to run the samples, but I only had SQL Server 2008 developer edition installed. I decided to give it a try anyways and immediately hit a snag when trying [...]]]></description>
			<content:encoded><![CDATA[<p>I downloaded the <a href="http://www.microsoft.com/azure/default.mspx" target="_self">Windows Azure SDK</a> today and was going through the<a href="http://msdn.microsoft.com/en-us/library/dd179419.aspx" target="_blank"> quick starts from MSDN</a> , the quickstarts mention that you need SQL Server Express 2005/2008 to run the samples, but I only had SQL Server 2008 developer edition installed. I decided to give it a try anyways and immediately hit a snag when trying to build the samples because as expected it could not find the SQLExpress instance that it was assuming would be present on the localhost.</p>
<p>Here are the changes I had to make to get the samples and the SDK to work with SQLServer 2008 installed without an instance name on my laptop.</p>
<p>1. In Windows Azure SDK\v1.0\samples\MSBuild\Microsoft.Samples.ServiceHosting.targets , add the &#8220;/server&#8221; option to the UpdateSampleTablesDB target to make it look like</p>
<blockquote><p>&lt;Target Name=&#8221;UpdateSamplesTableDB&#8221; DependsOnTargets=&#8221;BuildSubProjects&#8221;&gt;</p>
<p>    &lt;Message Text=&#8221;$(DevTableGenCommand) /database:$(SamplesDBName) /server:localhost $(DevtableGenForceCreateFlag) @(DevTableGenAssemblies)&#8221;/&gt;</p>
<p>    &lt;Exec Condition=&#8221;&#8216;$(SamplesDBName)&#8217;!=&#8221;&#8221; </p>
<p>            Command=&#8221;$(DevTableGenCommand) /database:$(SamplesDBName) <strong>/server:localhost</strong> $(DevtableGenForceCreateFlag) @(DevTableGenAssemblies)&#8221; </p>
<p>    WorkingDirectory=&#8221;$(MSBuildProjectDirectory)&#8221;/&gt;</p>
<p>  &lt;/Target&gt;</p></blockquote>
<p>2. Change the datasource in the connection string used in  &#8221;Windows Azure SDK\v1.0\bin\DevelopmentStorage.exe.config&#8221; to localhost</p>
<blockquote><p>&lt;connectionStrings&gt;</p>
<p>    &lt;add name=&#8221;DevelopmentStorageDbConnectionString&#8221;</p>
<p>         connectionString=&#8221;Data Source=<strong>localhost</strong>;Initial Catalog=DevelopmentStorageDb;Integrated Security=True&#8221;</p>
<p>         providerName=&#8221;System.Data.SqlClient&#8221; /&gt;</p>
<p>  &lt;/connectionStrings&gt;</p></blockquote>
<p>3. Change the dbServer for the Table service in the developmentStorageConfig section to localhost in &#8220;Windows Azure SDK\v1.0\bin\DevelopmentStorage.exe.config&#8221;</p>
<blockquote><p>    &lt;services&gt;</p>
<p>      &lt;service name=&#8221;Blob&#8221;</p>
<p>               url=&#8221;http://127.0.0.1:10000/&#8221;/&gt;</p>
<p>      &lt;service name=&#8221;Queue&#8221;</p>
<p>               url=&#8221;http://127.0.0.1:10001/&#8221;/&gt;</p>
<p>      &lt;service name=&#8221;Table&#8221;</p>
<p>               url=&#8221;http://127.0.0.1:10002/&#8221;</p>
<p><span> </span>         dbServer=&#8221;<strong>localhost</strong>&#8220;/&gt;</p>
<p> </p>
<p>    &lt;/services&gt;</p></blockquote>
<p>HTH</p>
]]></content:encoded>
			<wfw:commentRss>http://www.indiangeek.net/2008/10/29/using-windows-azure-with-sql-server-20052008/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.indiangeek.net/2008/10/29/using-windows-azure-with-sql-server-20052008/</feedburner:origLink></item>
		<item>
		<title>A case study in micro-optimization</title>
		<link>http://feeds.feedburner.com/~r/Indiangeek/~3/378198541/</link>
		<comments>http://www.indiangeek.net/2008/08/29/a-case-study-in-micro-optimization/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 15:17:22 +0000</pubDate>
		<dc:creator>sijin</dc:creator>
		
		<category><![CDATA[.Net]]></category>

		<category><![CDATA[Software Development]]></category>

		<category><![CDATA[algorithms]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[coding]]></category>

		<category><![CDATA[optimization]]></category>

		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.indiangeek.net/?p=393</guid>
		<description><![CDATA[Last week I saw a wrap up from cedric&#8217;s coding challenge on his blog, the problem looked simple enough, &#8220;write a counter function that counts from 1 to max but only returns numbers whose digits don&#8217;t repeat&#8221;.
My first stab at it consisted of a brute force solution of looking at every natural number up to [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I saw a wrap up from <a title="Cedric's Coding Challenge Wrapup" href="http://beust.com/weblog/archives/000493.html">cedric&#8217;s coding challenge</a> on his blog, the problem looked simple enough, <em><strong>&#8220;write a counter function that counts from 1 to max but only returns numbers whose digits don&#8217;t repeat&#8221;</strong></em>.</p>
<p>My first stab at it consisted of a brute force solution of looking at every natural number up to a given limit and determining if the digits were unique, the crux of which was this function that given a number determined if the digits repeated or not</p>
<pre class="syntax-highlight:c#">
private static bool DoDigitsRepeat(long num) {
····//Used to track which digits have already been encountered
····int used = 0;
····while (num &gt; 0) {
········int digit = (int)num % 10;
········num = num / 10;

········int index = 1 &lt;&lt; digit;
········if ((used &amp; index) == index) {
············return true;
········}
········else {
············used |= index;
········}
····}
····return false;
}
</pre>
<p>The good thing about this solution was that it met the secondary goal for the problem which was to determine the biggest gap in the sequence of generated numbers, but in terms of performance it sucked! It worked well for the smaller limits defined in the problem but when you tried to push and generate all matching numbers till the max possible i.e. 9876543210 it just took too long (I think it was under a minute, but that was still too long)</p>
<p>I went back to the problem page and saw that the scripting weenies who had tried to use string functions to prune numbers had the worst performance of all solutions, that made me feel a bit better until I saw <a title="Crazy Bob's wicked fast solution" href="http://crazybob.org/FastBeustSequence.java.html">CrazyBob&#8217;s Java solution(the fast version)</a>, the comments indicated that the solution found the total count in under half a second. This solution was not able to determine the biggest gap though because the numbers were generated out of order but nevertheless this was an excellent solution.</p>
<p>So began my quest to come up with a faster solution. I felt sure that I could use the <a title="Bit Twiddling Hacks" href="http://graphics.stanford.edu/~seander/bithacks.html">bit twiddling hacks</a> to come up with a faster solution, it was just a question of hitting the right spot.</p>
<p>The first non-brute force solution that I tried was to enumerate all possible subsets of {0,1,2,3,4,5,6,7,8,9} and then to generate permutations from that set making sure that zero was never in the first place. Subset generation is pretty easy to do, all you need to do is count from 0 to 2^n where n is the number of elements in the set, the bit patterns of all the numbers in this range can be used to generate all the subsets. For the first cut I tried to be Object-Oriented and used the below class to implement a BitTable.</p>
<pre class="syntax-highlight:c#">
public class BitTable
{
····private uint _storage = 0;
····public BitTable(uint value) {
········_storage = value;
····}

····public void Set(int index) {
········Debug.Assert(index &gt;= 0 &amp;&amp; index &lt; 32);
········_storage |= (uint)(1 &lt;&lt; index);
····}

····public void Reset(int index) {
········Debug.Assert(index &gt;= 0 &amp;&amp; index &lt; 32);
········_storage &amp;= (uint)~(1 &lt;&lt; index);
····}

····public bool IsSet(int index) {
········Debug.Assert(index &gt;= 0 &amp;&amp; index &lt; 32);
········uint val = (uint)(1 &lt;&lt; index);
········return (_storage &amp; val) == val;
····}
}
</pre>
<p>Of course as I soon found out, all the method calls to BitTable were really slowing things down (and by slow I mean a 1-2 seconds slower than CrazyBob&#8217;s solution), so I dropped the class and moved all the operations inline, also I realized that since I was using value types to hold the state in the search/call tree, I didn&#8217;t need to set and unset the state after each recursive call. Here&#8217;s the final version of this line of thinking.</p>
<pre class="syntax-highlight:c#">
class Beust4
{
private static int _total = 0;
public static void Run() {
····_total = 0;
····//Generate all possible subsets of a set of 10 elements
····//2^10 = 1024
····for (int i = 1; i &lt;= 1024; ++i) {
········Permute(i, 0L);
····}

····Console.WriteLine(&quot;nTotal: {0}&quot;, _total);
}

private static void Permute(int digits, long current) {
····for (int index = (current &gt; 0) ? 0 : 1; index &lt;= 9; ++index) {
········if ((digits &amp; (1 &lt;&lt; index)) == (1 &lt;&lt; index)) {
············if ((digits &amp; ~(1 &lt;&lt; index)) == 0) {
················//Console.Write(&quot;{0}, &quot;, (current * 10) + index);
················++_total;
················return;
············}
············else {
················Permute(digits &amp; ~(1 &lt;&lt; index), (current * 10) + index);
············}
········}
····}
}
</pre>
<p>I felt really good about this attempt but to my surprise when I ran it, I took 1.2 seconds on average which was still more than 2 times slower than CrazyBob&#8217;s Java solution. I got really stuck at this point and I had to make sure that there was not something obvious that I was missing. The first thing I did was to port CrazyBob&#8217;s solution to .Net so that I could compare both solutions, I&#8217;ve uploaded the <a title="C# version of CrazyBob's solution" href="http://pastebin.com/f9f714a4" target="_blank">C# version of CrazyBob&#8217;s solution </a>here in case anyone wants to take a look.</p>
<p>On the surface it looks like the bit twiddling based solution should run faster because it does not make all those method calls, the other thing that I suspected was causing problems was the number of recursive function calls that were being made, so I put in some code to check the number of recursive function calls that were being made and to my surprise I found that CrazyBob&#8217;s solution was making 8877691 recursive calls as compared to my solution which was making almost 10 times that number. Also the actual soultion count is 8877690 which meant that the number of calls in CrazyBob&#8217;s solution was near optimal. So it was clear that it was the number of calls that were costing me the half second. Btw CrazyBob&#8217;s C# version still ran in <strong>700ms</strong> on average on my laptop, which was still <strong>500ms</strong> faster than my C# version.</p>
<p>I then started to think about alternate ways to attack the issue, one track I went down was to consider all the digits as a complete graph and then coming up with a way to enumerate all paths in the graph, traversing a n edge in the graph would remove other edges from the graph and make them not available. This reminded me on <a title="Dancing Links Algorithm" href="http://en.wikipedia.org/wiki/Dancing_Links" target="_blank">Knuth&#8217;s Dancing Links Algorithm</a> and I read up on that a bit, this <a title="Dancing Links" href="http://lanl.arxiv.org/pdf/cs/0011047" target="_blank">paper from Knuth</a> on the subject was an excellent read. It looked to me that CrazyBob had used an approach similar to the DLX algorithm, but after reading the entire paper from Knuth it still didn&#8217;t strike me as to why using a DLX approach would provide such excellent performance as compared to my version.</p>
<p>So I went back a bit to comparing both solutions and I think the two key observations that I saw that</p>
<ol>
<li>CrazyBob&#8217;s solution went down the search tree to one level above the last  and then just generated all the solution from there instead of recursing down to the last level, so for e.g. supposing it was generating 5 digit numbers and it had already generated 4321, then at that level it didn&#8217;t make additional recursive calls to add the final digit, it was able to add the last digit at the same level pruning the search tree quite a bit. In contrast my solution was basically doing a method call for every digit of every number in the solution set.</li>
<li>The above optimization was made possible by using the length of the final solution as the key, so first all 1 digit solutions were generated, followed by two digit ones and so on.</li>
</ol>
<p>Cool, so now I ported my bit twiddling version to generate based on the length of the numbers and the optimization to prune the search tree one level above the last came naturally. I ran my solution and guess what, it was still 200 ms slower than CrazyBob <img src='http://www.indiangeek.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> Aaaarghhhh!!!!!</p>
<pre class="syntax-highlight:c#">
for (int len = 1; len &lt;= 10; ++len) {
····Generate(len, 0, 0xFFF &gt;&gt; 2, 0);
}

private static void Generate(int maxLen, int currentLen, int availableDigits, long currentValue) {
····bool last = (currentLen == maxLen - 1);
····for (int digit = (currentValue == 0) ? 1 : 0; digit &lt;= 9; ++digit) {
········if ((availableDigits &amp; (1 &lt;&lt; digit)) != (1 &lt;&lt; digit))
············continue;

········if (last) {
············++_total;
············//Console.Write(&quot;{0}, &quot;, (currentValue * 10) + i);
········}
········else {
············Generate(maxLen, currentLen + 1, availableDigits &amp; ~(1 &lt;&lt; digit), (currentValue * 10) + digit);
········}
····}
}
</pre>
<p>But I knew I was getting close, at this point I knew what was killing me basically to determine which bits were set I was iterating from 0 to 9 and then testing if that bit was set in the number or not, this test was killing me because most of the times the bit was not set and I was doing a huge huge number of unnecessary tests. So I needed a way to iterate through only the set bits. The first solution I tried used a hashtable but that caused a even bigger degradation in performance. Finally for a lack of a better way to express this in C# I had to waste 4KB of memory and use an array to allow me to iterate through the indexes of the set bits in a number.</p>
<p>The final solution ran in 350ms on average, almost a 50% improvement over CrazyBob&#8217;s solution, woot!!!!! A further optimzation to move the if statement from inside the loop to outside which makes the code more readable but incredibly smelly because of the near duplicate code in both the if and else blocks shaved off another 50ms. Here&#8217;s the final version without the optimzation for moving the if statement outside which makes the code a bit shorter.</p>
<pre class="syntax-highlight:c#">
class Beust5
{
····private static int _total = 0;
····private static int[] _pre = null;
····public static void Run() {
········_total = 0;
········_pre = new int[(1 &lt;&lt; 10) + 1];
········for (int i = 0; i &lt;= 10; ++i) {
············_pre[1 &lt;&lt; i] = i;
········}

········for (int len = 1; len &lt;= 10; ++len) {
············Generate2(len, 0, 0xFFF &gt;&gt; 2, 0);
········}

········Console.WriteLine(&quot;nTotal: {0}&quot;, _total);
····}

····private static void Generate2(int maxLen, int currentLen, int availableDigits, long currentValue) {
········bool last = (currentLen == maxLen - 1);
········int x = availableDigits;
········while (x != 0) {
············//digit will contain the lowest set bit
············int digit = _pre[x ^ (x &amp; (x - 1))];
············x &amp;= (x - 1);

············//Avoid starting with zero
············if (digit == 0 &amp;&amp; currentValue == 0)
················continue;

············if (last) {
················++_total;
················//Console.Write(&quot;{0}, &quot;, (currentValue * 10) + i);
············}
············else {
················Generate2(maxLen, currentLen + 1, availableDigits &amp; ~(1 &lt;&lt; digit), (currentValue * 10) + digit);
············}
········}
····}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.indiangeek.net/2008/08/29/a-case-study-in-micro-optimization/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.indiangeek.net/2008/08/29/a-case-study-in-micro-optimization/</feedburner:origLink></item>
		<item>
		<title>links for 2008-07-30</title>
		<link>http://feeds.feedburner.com/~r/Indiangeek/~3/350963860/</link>
		<comments>http://www.indiangeek.net/2008/07/30/links-for-2008-07-30/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 23:32:17 +0000</pubDate>
		<dc:creator>sijin</dc:creator>
		
		<category><![CDATA[Misc]]></category>

		<guid isPermaLink="false">http://www.indiangeek.net/2008/07/30/links-for-2008-07-30/</guid>
		<description><![CDATA[

UPDATED: 10 Must-Have Python Packages for Social Scientists  Zero Intelligence Agents
(tags: tools software research python reference)


Edge: ENGINEERS&#8217; DREAMS By George Dyson
(tags: toread story society slashdot singularity scifi google interesting)


OpenRemote - Home
The Home of the Internet-enabled Home. We are an Open Community in the Home Automation and Domotics space. We believe an Open Source approach can revolutionize [...]]]></description>
			<content:encoded><![CDATA[<ul class="delicious">
<li>
<div class="delicious-link"><a href="http://blogs.nyu.edu/blogs/agc282/zia/2008/06/10_musthaveknow_python_package.html">UPDATED: 10 Must-Have Python Packages for Social Scientists <> Zero Intelligence Agents</a></div>
<div class="delicious-tags">(tags: <a href="http://del.icio.us/sijin/tools">tools</a> <a href="http://del.icio.us/sijin/software">software</a> <a href="http://del.icio.us/sijin/research">research</a> <a href="http://del.icio.us/sijin/python">python</a> <a href="http://del.icio.us/sijin/reference">reference</a>)</div>
</li>
<li>
<div class="delicious-link"><a href="http://www.edge.org/3rd_culture/dysong08/dysong08_index.html">Edge: ENGINEERS&#8217; DREAMS By George Dyson</a></div>
<div class="delicious-tags">(tags: <a href="http://del.icio.us/sijin/toread">toread</a> <a href="http://del.icio.us/sijin/story">story</a> <a href="http://del.icio.us/sijin/society">society</a> <a href="http://del.icio.us/sijin/slashdot">slashdot</a> <a href="http://del.icio.us/sijin/singularity">singularity</a> <a href="http://del.icio.us/sijin/scifi">scifi</a> <a href="http://del.icio.us/sijin/google">google</a> <a href="http://del.icio.us/sijin/interesting">interesting</a>)</div>
</li>
<li>
<div class="delicious-link"><a href="http://openremote.org/">OpenRemote - Home</a></div>
<div class="delicious-extended">The Home of the Internet-enabled Home. We are an Open Community in the Home Automation and Domotics space. We believe an Open Source approach can revolutionize the way people create, install, and maintain software in the industry.</div>
<div class="delicious-tags">(tags: <a href="http://del.icio.us/sijin/tools">tools</a> <a href="http://del.icio.us/sijin/technology">technology</a> <a href="http://del.icio.us/sijin/software">software</a> <a href="http://del.icio.us/sijin/remote">remote</a> <a href="http://del.icio.us/sijin/openremote">openremote</a> <a href="http://del.icio.us/sijin/hardware">hardware</a> <a href="http://del.icio.us/sijin/java">java</a> <a href="http://del.icio.us/sijin/linux">linux</a>)</div>
</li>
<li>
<div class="delicious-link"><a href="https://networkx.lanl.gov/wiki">NetworkX</a></div>
<div class="delicious-extended">NetworkX (NX) is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks.</div>
<div class="delicious-tags">(tags: <a href="http://del.icio.us/sijin/visualization">visualization</a> <a href="http://del.icio.us/sijin/tools">tools</a> <a href="http://del.icio.us/sijin/software">software</a> <a href="http://del.icio.us/sijin/socialnetworks">socialnetworks</a> <a href="http://del.icio.us/sijin/python">python</a> <a href="http://del.icio.us/sijin/research">research</a> <a href="http://del.icio.us/sijin/science">science</a>)</div>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.indiangeek.net/2008/07/30/links-for-2008-07-30/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.indiangeek.net/2008/07/30/links-for-2008-07-30/</feedburner:origLink></item>
		<item>
		<title>links for 2008-07-29</title>
		<link>http://feeds.feedburner.com/~r/Indiangeek/~3/349919827/</link>
		<comments>http://www.indiangeek.net/2008/07/29/links-for-2008-07-29/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 23:32:03 +0000</pubDate>
		<dc:creator>sijin</dc:creator>
		
		<category><![CDATA[Misc]]></category>

		<guid isPermaLink="false">http://www.indiangeek.net/2008/07/29/links-for-2008-07-29/</guid>
		<description><![CDATA[

GoGrid :: Scalable Load-Balanced Windows and Linux Cloud-Server Hosting
GoGrid is the world&#8217;s first multi-server control panel that enables you to deploy and manage load-balanced cloud servers in just minutes.
(tags: hosting grid cloud scaling cluster computing server)


]]></description>
			<content:encoded><![CDATA[<ul class="delicious">
<li>
<div class="delicious-link"><a href="http://www.gogrid.com/index.php">GoGrid :: Scalable Load-Balanced Windows and Linux Cloud-Server Hosting</a></div>
<div class="delicious-extended">GoGrid is the world&#8217;s first multi-server control panel that enables you to deploy and manage load-balanced cloud servers in just minutes.</div>
<div class="delicious-tags">(tags: <a href="http://del.icio.us/sijin/hosting">hosting</a> <a href="http://del.icio.us/sijin/grid">grid</a> <a href="http://del.icio.us/sijin/cloud">cloud</a> <a href="http://del.icio.us/sijin/scaling">scaling</a> <a href="http://del.icio.us/sijin/cluster">cluster</a> <a href="http://del.icio.us/sijin/computing">computing</a> <a href="http://del.icio.us/sijin/server">server</a>)</div>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.indiangeek.net/2008/07/29/links-for-2008-07-29/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.indiangeek.net/2008/07/29/links-for-2008-07-29/</feedburner:origLink></item>
		<item>
		<title>links for 2008-07-27</title>
		<link>http://feeds.feedburner.com/~r/Indiangeek/~3/347831381/</link>
		<comments>http://www.indiangeek.net/2008/07/27/links-for-2008-07-27/#comments</comments>
		<pubDate>Sun, 27 Jul 2008 23:33:18 +0000</pubDate>
		<dc:creator>sijin</dc:creator>
		
		<category><![CDATA[Misc]]></category>

		<guid isPermaLink="false">http://www.indiangeek.net/2008/07/27/links-for-2008-07-27/</guid>
		<description><![CDATA[

Game/AI: Fixing Pathfinding Once and For All
(tags: ai programming pathfinding games algorithm article)


Alice.org
Alice is an innovative 3D programming environment that makes it easy to create an animation for telling a story, playing an interactive game, or a video to share on the web. Alice is a teaching tool for introductory computing.
(tags: programming education software alice [...]]]></description>
			<content:encoded><![CDATA[<ul class="delicious">
<li>
<div class="delicious-link"><a href="http://www.ai-blog.net/archives/000152.html">Game/AI: Fixing Pathfinding Once and For All</a></div>
<div class="delicious-tags">(tags: <a href="http://del.icio.us/sijin/ai">ai</a> <a href="http://del.icio.us/sijin/programming">programming</a> <a href="http://del.icio.us/sijin/pathfinding">pathfinding</a> <a href="http://del.icio.us/sijin/games">games</a> <a href="http://del.icio.us/sijin/algorithm">algorithm</a> <a href="http://del.icio.us/sijin/article">article</a>)</div>
</li>
<li>
<div class="delicious-link"><a href="http://www.alice.org/index.php">Alice.org</a></div>
<div class="delicious-extended">Alice is an innovative 3D programming environment that makes it easy to create an animation for telling a story, playing an interactive game, or a video to share on the web. Alice is a teaching tool for introductory computing.</div>
<div class="delicious-tags">(tags: <a href="http://del.icio.us/sijin/programming">programming</a> <a href="http://del.icio.us/sijin/education">education</a> <a href="http://del.icio.us/sijin/software">software</a> <a href="http://del.icio.us/sijin/alice">alice</a> <a href="http://del.icio.us/sijin/development">development</a> <a href="http://del.icio.us/sijin/computer">computer</a>)</div>
</li>
<li>
<div class="delicious-link"><a href="http://www.nickstakenburg.com/projects/prototip2/">Prototip 2 - Create beautiful tooltips with ease</a></div>
<div class="delicious-tags">(tags: <a href="http://del.icio.us/sijin/javascript">javascript</a> <a href="http://del.icio.us/sijin/prototype">prototype</a> <a href="http://del.icio.us/sijin/tooltip">tooltip</a> <a href="http://del.icio.us/sijin/ajax">ajax</a> <a href="http://del.icio.us/sijin/tooltips">tooltips</a> <a href="http://del.icio.us/sijin/css">css</a> <a href="http://del.icio.us/sijin/webdesign">webdesign</a> <a href="http://del.icio.us/sijin/design">design</a>)</div>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.indiangeek.net/2008/07/27/links-for-2008-07-27/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.indiangeek.net/2008/07/27/links-for-2008-07-27/</feedburner:origLink></item>
		<item>
		<title>links for 2008-07-23</title>
		<link>http://feeds.feedburner.com/~r/Indiangeek/~3/344027279/</link>
		<comments>http://www.indiangeek.net/2008/07/23/links-for-2008-07-23/#comments</comments>
		<pubDate>Wed, 23 Jul 2008 23:33:00 +0000</pubDate>
		<dc:creator>sijin</dc:creator>
		
		<category><![CDATA[Misc]]></category>

		<guid isPermaLink="false">http://www.indiangeek.net/2008/07/23/links-for-2008-07-23/</guid>
		<description><![CDATA[

mini-itx.com - news
(tags: hardware mini-itx computer computers diy)


]]></description>
			<content:encoded><![CDATA[<ul class="delicious">
<li>
<div class="delicious-link"><a href="http://www.mini-itx.com/">mini-itx.com - news</a></div>
<div class="delicious-tags">(tags: <a href="http://del.icio.us/sijin/hardware">hardware</a> <a href="http://del.icio.us/sijin/mini-itx">mini-itx</a> <a href="http://del.icio.us/sijin/computer">computer</a> <a href="http://del.icio.us/sijin/computers">computers</a> <a href="http://del.icio.us/sijin/diy">diy</a>)</div>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.indiangeek.net/2008/07/23/links-for-2008-07-23/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.indiangeek.net/2008/07/23/links-for-2008-07-23/</feedburner:origLink></item>
		<item>
		<title>links for 2008-07-21</title>
		<link>http://feeds.feedburner.com/~r/Indiangeek/~3/342002559/</link>
		<comments>http://www.indiangeek.net/2008/07/21/links-for-2008-07-21/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 23:34:37 +0000</pubDate>
		<dc:creator>sijin</dc:creator>
		
		<category><![CDATA[Misc]]></category>

		<guid isPermaLink="false">http://www.indiangeek.net/2008/07/21/links-for-2008-07-21/</guid>
		<description><![CDATA[

Drop.io: Simple Private Sharing
Drop.io enables you to create simple private sharing points called &#8220;drops.&#8221;
The service has no email signup and no &#8220;accounts.&#8221; Each drop is private, and only as accessible as you choose to deliberately make it.
(tags: filesharing storage sharing free tools online)


]]></description>
			<content:encoded><![CDATA[<ul class="delicious">
<li>
<div class="delicious-link"><a href="http://drop.io/">Drop.io: Simple Private Sharing</a></div>
<div class="delicious-extended">Drop.io enables you to create simple private sharing points called &#8220;drops.&#8221;</p>
<p>The service has no email signup and no &#8220;accounts.&#8221; Each drop is private, and only as accessible as you choose to deliberately make it.</p></div>
<div class="delicious-tags">(tags: <a href="http://del.icio.us/sijin/filesharing">filesharing</a> <a href="http://del.icio.us/sijin/storage">storage</a> <a href="http://del.icio.us/sijin/sharing">sharing</a> <a href="http://del.icio.us/sijin/free">free</a> <a href="http://del.icio.us/sijin/tools">tools</a> <a href="http://del.icio.us/sijin/online">online</a>)</div>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.indiangeek.net/2008/07/21/links-for-2008-07-21/feed/</wfw:commentRss>
		<feedburner:origLink>http://www.indiangeek.net/2008/07/21/links-for-2008-07-21/</feedburner:origLink></item>
	</channel>
</rss>
