<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Li Chen's Blog</title><link>http://weblogs.asp.net/lichen/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/lichen" /><feedburner:info uri="lichen" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><title>modern.ie</title><link>http://feedproxy.google.com/~r/lichen/~3/KIUaCWk6lbc/modern-ie.aspx</link><pubDate>Fri, 17 May 2013 01:33:21 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:10298491</guid><dc:creator>dotneteer</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=10298491</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2013/05/17/modern-ie.aspx#comments</comments><description>&lt;p&gt;We are planning to update our very old web application so that it works better with modern web browsers. I have heard of &lt;a href="http://modern.ie"&gt;modern.ie&lt;/a&gt; in the past and decided to give it a try. The site has a scanner that can scan a URL entered by a user. Unfortunately, our site requires log-in before getting to the page we want to scan. Fortunately, the site also offers a &lt;a href="http://virtualization.modern.ie/vhd/modern.ie.zip?10e3374d847343a78531a3fd386598a8"&gt;downloadable scanner&lt;/a&gt; that we can use in our local development environment.&lt;/p&gt;  &lt;p&gt;I downloaded the scanner. It is packaged as a zip file. I unzipped the package and open the readme file. The tools requires node.js to work. Fortunately, it is fairly easy to setup the tool following the instructions. Firstly, download and install &lt;a href="http://nodejs.org/"&gt;node.js&lt;/a&gt;; accept all defaults. The setup will add Node.js to the path. Although Node.js is known for hosting asynchronous web applications, it is actually just a javascript executing environment. Next, I open the command prompt, change to the directory that I unzipped the modern.ie tool and run “node lib/service.js”. A node hosted web server is listening on port 1337 so we just need to open a web browser and points to &lt;a title="http://localhost:1337/" href="http://localhost:1337/"&gt;http://localhost:1337/&lt;/a&gt;. We can then enter the URL of a web page to scan. The scanner submits the data to modern.ie and a report is generated. The report is very good and it offers many suggestions and links that helps us to improve the page.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=10298491" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/KIUaCWk6lbc" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/lichen/archive/tags/Javascript/default.aspx">Javascript</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2013/05/17/modern-ie.aspx</feedburner:origLink></item><item><title>Is it time for cloud-based ASP.NET IDE?</title><link>http://feedproxy.google.com/~r/lichen/~3/8n4VqFdCnBE/is-it-time-for-cloud-based-asp-net-ide.aspx</link><pubDate>Mon, 18 Mar 2013 05:54:41 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:10007097</guid><dc:creator>dotneteer</dc:creator><slash:comments>6</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=10007097</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2013/03/18/is-it-time-for-cloud-based-asp-net-ide.aspx#comments</comments><description>&lt;p&gt;We already have a standard IDE in Visual Studio. We also have a very innovative IDE in &lt;a href="http://www.microsoft.com/web/webmatrix/"&gt;WebMatrix&lt;/a&gt; that can work with node.js and php in addition to ASP.NET. Why do we need another IDE? Well, let me first talk about the feasibility and then throw in some ideas on what we could do with it.&lt;/p&gt;  &lt;p&gt;I have noticed &lt;a href="http://codemirror.net/"&gt;CodeMirror&lt;/a&gt; for a long time. Recently, I have also noticed &lt;a href="http://ace.ajax.org/"&gt;ACE&lt;/a&gt; on which the &lt;a href="https://c9.io/"&gt;Cloud9 IDE&lt;/a&gt; is based. I was impressed that both CodeMirror and ACE supported &lt;a href="http://www.typescriptlang.org/"&gt;Typescript&lt;/a&gt; soon after it was released. I was also impressed with the Cloud9 IDE that can debug Node.js code. All these projects have benefited from rapid improvement of the Javascript engine in modern browsers and I think that the era for very functional web-based IDEs has arrived.&lt;/p&gt;  &lt;p&gt;So what can we do with a cloud-based IDE? Firstly, like what Cloud9 demonstrated, the code lives on the cloud. Secondly, people can collaborate. Thirdly, I think it is possible to improve experience to what we have never seen before, so let me elaborate below.&lt;/p&gt;  &lt;p&gt;In Visual Studio, when we want to see how the ASP.NET page looks like, we switch to the designer mode but that is not really close to what we see at runtime. In a cloud-based IDE, we can see the code and how it renders side-by-side, like what &lt;a href="http://jsfiddle.net/"&gt;JsFiddle&lt;/a&gt; has demonstrated. ASP.NET does not execute the code on the website directly; it first compiles the code into the “Temporary ASP.NET Files” directory and executes the assemblies from there. That makes it feasible to edit and run the code at the same time.&lt;/p&gt;  &lt;p&gt;Visual Studio has been heavy on ORM (i.e., entity framework) and lighter on code-generation. I have been favoring code-generation over ORM. This is because with ORM we are tweaking a black box to generate desired SQL. With code-generation, we can see exactly what we get and tweak the templates when necessary. Visual Studio has limited runtime information; it gets its information either from parsing the code, or from meta-data stored in XML files. With a cloud-based IDE that is working with running code, it is possible to get richer runtime information and generate code (or scaffold) under much wider scenarios. So how do we map the server-side code to html in the browser? That is where ideas like &lt;a href="https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit?pli=1#!"&gt;source map&lt;/a&gt; could help, and we have very good tools in querying DOM already.&lt;/p&gt;  &lt;p&gt;So I believe all the technical pieces for a cloud-based ASP.NET IDE are available. &lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=10007097" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/8n4VqFdCnBE" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/ASP.NET/default.aspx">ASP.NET</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2013/03/18/is-it-time-for-cloud-based-asp-net-ide.aspx</feedburner:origLink></item><item><title>Lessons from the ASP Classic Compiler project</title><link>http://feedproxy.google.com/~r/lichen/~3/6I34awHVTyM/lessons-from-the-asp-classic-compiler-project.aspx</link><pubDate>Mon, 18 Mar 2013 04:31:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:10006776</guid><dc:creator>dotneteer</dc:creator><slash:comments>2</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=10006776</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2013/03/18/lessons-from-the-asp-classic-compiler-project.aspx#comments</comments><description>&lt;p&gt;I have not done much with my &lt;a href="http://aspclassiccompiler.codeplex.com/" mce_href="http://aspclassiccompiler.codeplex.com/"&gt;ASP Classic Compiler&lt;/a&gt; project for over a year now. The lack of additional work is due to both the economic and the technical reasons. I will try to document the reasons here both for myself and for would–be open source developers.&lt;/p&gt;  &lt;h3&gt;How the project got started?&lt;/h3&gt;  &lt;p&gt;I joint my current employer at the end of 2008. The company had a large amount of ASP classic code in its core product. The company was in talk with a major client on customization so that there was a period that the development team had fairly light load. I had a chance to put in a lot of thinking on how to convert the ASP classic code to ASP.NET. One of the ideas was to compile ASP Classic code into .net IL so that they can be executed within the ASP.NET runtime.&lt;/p&gt;  &lt;p&gt;I do not have a formal education in computer science; my Ph.D. is in physics. I had been very fond of writing parsers by following the book “&lt;a href="http://www.amazon.com/gp/product/0471113530/ref=as_li_qf_sp_asin_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0471113530&amp;amp;linkCode=as2&amp;amp;tag=wwwdotneteeco-20" mce_href="http://www.amazon.com/gp/product/0471113530/ref=as_li_qf_sp_asin_tl?ie=UTF8&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0471113530&amp;amp;linkCode=as2&amp;amp;tag=wwwdotneteeco-20"&gt;Writing Compilers &amp;amp; Interpreters&lt;/a&gt;” since 1996. I wrote a VBA like interpreter and a mini web-browser, all in Visual Basic, using the knowledge acquired. Nevertheless, a full-featured compiler is still a major undertaking. I spent several months of my spare-time working on the parser and compiler, and studied the theory behind. I was able to implement all the VBScript 3.0 features and the compiler was able to execute several Microsoft best-practice applications. The code runs about twice as fast as ASP Classic. I had lots of fun experimenting with ideas such as using StringBuilder for string concatenation to drastically improve the speed of rendering. I was awarded Microsoft ASP/ASP.NET MVP in 2010. In 2011, I donated the project to the community by opening the source code after consulting with my employer.&lt;/p&gt;  &lt;p&gt;Since then, I faced challenges on two fronts: adding VBScript 5.0 support and solving a large number of compatibility issues in the real-world. After working for a few more months, I found it difficult to sustain the project.&lt;/p&gt;  &lt;h3&gt;Lesson 1: the economy and business issues&lt;/h3&gt;  &lt;p&gt;1. Sustainability of an open source project requires business arrangements. I am an employee. Technical activities outside the work are welcome as long as it benefits the employer. However, excess amount of activities not relating to the work could be a loyalty issue.&lt;/p&gt;  &lt;p&gt;2. ASP Classic is a shrinking platform. So ASP Classic Compiler would not be a sound investment for most businesses except for Microsoft who has interest in bridging customers from older to newer platform painlessly.&lt;/p&gt;  &lt;h3&gt;Lesson 2: the technical issues&lt;/h3&gt;  &lt;p&gt;1. VBScript is a terrible language without a formal specification and test suites. The best “spec” outside of the users’ guide is probably &lt;a href="http://blogs.msdn.com/b/ericlippert/" mce_href="http://blogs.msdn.com/b/ericlippert/"&gt;Eric Lippert’s blog&lt;/a&gt;. It is not getting much maintenance and it is losing favor. As a result, few other developers with knowledge in compiler would have interest in VBScript.&lt;/p&gt;  &lt;p&gt;2. Like the IronPython project and the Rhino projects, I started by writing a compiler. I was very shrilled that my first implementation without much optimization is about twice as fast as VBScript in my benchmark test. However, I had many obstacles on compatibility and adding the debug feature. If I would do it again, I would probably implement it as an interpreter with an incremental compiler. The interpreter would have a smaller start-up overhead. I can then have a background thread compiling the high usage code. The delayed compiling would allow me to employee more sophisticated data-flow analysis and construct larger call-site blocks. The current naïve implementation on DLR results in too many very small call-sites so that type-checking at call-sites is a significant overhead. &lt;/p&gt;  &lt;p&gt;In summary, I had lots of fun with the project. I significantly improved my theoretical knowledge of algorithms in general, and of parsers and compilers. I had the honor of being awarded Microsoft ASP.NET/IIS MVP for the past 3 years and enjoyed my private copy of MSDN subscription at home. After a working prototype, the remaining work to take it to a production quality software is more a business problem than else. I am actively contemplating some new forward-looking ideas and wish I have some results to share soon.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=10006776" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/6I34awHVTyM" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/lichen/archive/tags/ASP+Classic+Compiler/default.aspx">ASP Classic Compiler</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2013/03/18/lessons-from-the-asp-classic-compiler-project.aspx</feedburner:origLink></item><item><title>ASP.NET and Open Source</title><link>http://feedproxy.google.com/~r/lichen/~3/f3jI1dv8fSc/asp-net-and-open-source.aspx</link><pubDate>Sat, 23 Feb 2013 06:35:13 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9903354</guid><dc:creator>dotneteer</dc:creator><slash:comments>2</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=9903354</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2013/02/23/asp-net-and-open-source.aspx#comments</comments><description>&lt;p&gt;I just came back from the Microsoft MVP Summit 2013. I was surprised and excited to learn there are a large number of open source projects from both inside and outside of Microsoft. There is also a strong support for open source frameworks in Visual Studio. I am glad to see that the Microsoft ASP.NET team has done a great job supporting open source and the community is going strong.&lt;/p&gt;  &lt;h3&gt;Open source projects from the ASP.NET team&lt;/h3&gt;  &lt;p&gt;Those who interested in the open source projects from the ASP.NET team should first visit &lt;a title="http://aspnet.codeplex.com/" href="http://aspnet.codeplex.com/"&gt;http://aspnet.codeplex.com/&lt;/a&gt;. This site is a portal to many ASP.NET features that Microsoft has opened the source code.&lt;/p&gt;  &lt;p&gt;Next, readers should visit &lt;a title="http://aspnetwebstack.codeplex.com/" href="http://aspnetwebstack.codeplex.com/"&gt;http://aspnetwebstack.codeplex.com/&lt;/a&gt;. This site is the home of the latest source code of MVC, Web API and Web Pages.&lt;/p&gt;  &lt;p&gt;The following are several open source projects that have been incorporated into Visual Studio:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a title="http://nuget.codeplex.com/" href="http://nuget.codeplex.com/"&gt;http://nuget.codeplex.com/&lt;/a&gt;: Nuget is now distributed with Visual Studio 2012.&lt;/li&gt;    &lt;li&gt;&lt;a title="https://github.com/SignalR" href="https://github.com/SignalR"&gt;https://github.com/SignalR&lt;/a&gt;: SignalR is a framework for building long polling application by Damian Edwards and David Fowler. As of &lt;a href="http://www.asp.net/vnext"&gt;ASP.NET and Web Tools 2012.2&lt;/a&gt;, SignalR is part of ASP.NET and a Microsoft supported project. Don’t forget to change the namespace if you are using an earlier version. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The following projects are considered experimental:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a title="http://owin.org/" href="http://owin.org/"&gt;http://owin.org/&lt;/a&gt;: A specification of standard interface between .NET web servers and web applications.&lt;/li&gt;    &lt;li&gt;&lt;a title="http://katanaproject.codeplex.com/" href="http://katanaproject.codeplex.com/"&gt;http://katanaproject.codeplex.com/&lt;/a&gt;: An implementation of OWIN.&lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Projects from outside of Microsoft&lt;/h3&gt;  &lt;p&gt;If you think there are no needs for another framework since ASP.NET MVC is already great, you would be surprised to find out some open source alternatives have actually attracted many followers:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a title="http://nancyfx.org/" href="http://nancyfx.org/"&gt;http://nancyfx.org/&lt;/a&gt;: Look at what they have built and number of contributors.&lt;/li&gt;    &lt;li&gt;&lt;a title="http://mvc.fubu-project.org/" href="http://mvc.fubu-project.org/"&gt;http://mvc.fubu-project.org/&lt;/a&gt;: Another MVC framework that has a large number of contributors.&lt;/li&gt;    &lt;li&gt;The OWIN home page (&lt;a title="http://owin.org/" href="http://owin.org/"&gt;http://owin.org/&lt;/a&gt;) has links to other projects.&lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Open Source Client-Side MVC or MVVM frameworks supported by Microsoft Visual Studio&lt;/h3&gt;  &lt;p&gt;Microsoft &lt;a href="http://www.asp.net/vnext"&gt;ASP.NET and Web Tools 2012.2&lt;/a&gt; comes with a single page application (SPA) project template that uses &lt;a href="http://knockoutjs.com/"&gt;KnockoutJS&lt;/a&gt;. However, Mads Kristensen also built projects templates for several other highly popular JavaScript SPA libraries and frameworks: &lt;a href="http://www.breezejs.com/?utm_source=ms-spa"&gt;Breeze&lt;/a&gt;, &lt;a href="http://emberjs.com/"&gt;EmberJS&lt;/a&gt;, &lt;a href="http://durandaljs.com/"&gt;DurandalJS&lt;/a&gt; and &lt;a href="https://github.com/johnpapa/HotTowel"&gt;Hot Towel&lt;/a&gt;. Visit &lt;a title="http://www.asp.net/single-page-application/overview/templates" href="http://www.asp.net/single-page-application/overview/templates"&gt;http://www.asp.net/single-page-application/overview/templates&lt;/a&gt; for download links.&amp;#160; &lt;/p&gt;  &lt;p&gt;Don’t forget to install Mads’ &lt;a href="http://vswebessentials.com/"&gt;Web Essentials extension for Visual Studio 2012&lt;/a&gt;. You will be pleasantly surprised by the number of features that this extension adds to Visual Studio 2012.&lt;/p&gt;  &lt;h3&gt;Final Notes&lt;/h3&gt;  &lt;p&gt;I gathered these links for myself and others. If I missed any links, please post a comment or &lt;a href="http://weblogs.asp.net/lichen/contact.aspx"&gt;send me a note&lt;/a&gt;. I will be glad to update this page.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9903354" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/f3jI1dv8fSc" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/lichen/archive/tags/ASP.NET/default.aspx">ASP.NET</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2013/02/23/asp-net-and-open-source.aspx</feedburner:origLink></item><item><title>Build a dual-monitor stand for under $20</title><link>http://feedproxy.google.com/~r/lichen/~3/JY68zmv-fL8/build-a-dual-monitor-stand-for-under-20.aspx</link><pubDate>Sun, 06 Jan 2013 16:42:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9708873</guid><dc:creator>dotneteer</dc:creator><slash:comments>1</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=9708873</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2013/01/06/build-a-dual-monitor-stand-for-under-20.aspx#comments</comments><description>&lt;p mce_keep="true"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;Like many software developers, my primary computer is a laptop. At
home, I like to connect it to an external monitor and use the laptop screen as
the secondary monitor. I like to bring the laptop screen as close to my eye as
possible. I got an idea when I visited IKEA with my wife yesterday.&lt;!--?xml:namespace prefix = "o"--&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p mce_keep="true"&gt;&lt;font face="Times New Roman" size="3"&gt;
&lt;/font&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;IKEA carries a wide variety of legs and boards. I bought a box of
&lt;a href="http://www.ikea.com/us/en/search/?query=capita+leg" mce_href="http://www.ikea.com/us/en/search/?query=capita+leg"&gt;four 4” legs for $10&lt;/a&gt; (the website shows $14 but I got mine in the store for $10) and a &lt;a href="http://www.ikea.com/us/en/catalog/products/40154316/#/90154314" mce_href="http://www.ikea.com/us/en/catalog/products/40154316/#/90154314"&gt;14” by 46” pine wood board&lt;/a&gt; for $7.50. After a simple
assembly, I got my sturdy dual-monitor stand.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p mce_keep="true"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;img width="639" height="360" title="Dual-monitor stand" style="width: 639px; height: 360px;" alt="Dual-monitor stand" src="http://weblogs.asp.net/blogs/lichen/WP_20130106_001.jpg" mce_src="http://weblogs.asp.net/blogs/lichen/WP_20130106_001.jpg"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p mce_keep="true"&gt;&lt;font face="Times New Roman" size="3"&gt;
&lt;/font&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;I place both my wireless keyboard and mouse partially
underneath the stand. That allows me bring the laptop closer. The space underneath
the stand can be used as temporary storage space. This simple idea 
worked pretty well for me.&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p mce_keep="true"&gt;&lt;font face="Times New Roman" size="3"&gt;
&lt;/font&gt;&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9708873" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/JY68zmv-fL8" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/General+Software+Development/default.aspx">General Software Development</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2013/01/06/build-a-dual-monitor-stand-for-under-20.aspx</feedburner:origLink></item><item><title>Importing a large file into Sql Server database employing some statistics</title><link>http://feedproxy.google.com/~r/lichen/~3/kcA1d_vRH6c/importing-a-large-file-into-sql-server-database-with-machine-learning.aspx</link><pubDate>Fri, 04 Jan 2013 22:14:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9701122</guid><dc:creator>dotneteer</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=9701122</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2013/01/04/importing-a-large-file-into-sql-server-database-with-machine-learning.aspx#comments</comments><description>&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin: 0in 0in 10pt;" class="MsoNormal"&gt;&lt;span style='line-height: 115%; font-family: "Arial","sans-serif"; font-size: 10pt;'&gt;I imported a 2.5 GB file into our SQL server today. What
made this import challenging is SQL Server Import and Export Wizard failed to
read the data. This file is a tab delimited file with CR/LF as record
separator. The lines are truncated if the remaining line is empty, resulting
variable number of columns in each record. Also, there is a memo field that contains
CR/LF so that it is not reliable to use CR/LF to break the records.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin: 0in 0in 10pt;" class="MsoNormal"&gt;&lt;span style='line-height: 115%; font-family: "Arial","sans-serif"; font-size: 10pt;'&gt;I had to write my own import utility. The utility does
the import in 2 phases: analysis and import phases. In the first phase, the
utility reads the entire file to determine the number of columns, the data type
of each column, its width and whether it allows null. In order to address the
CR/LF problem, the utility uses the following algorithm:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin: 0in 0in 10pt;" class="MsoNormal"&gt;&lt;span style='line-height: 115%; font-family: "Arial","sans-serif"; font-size: 10pt;'&gt;Read a line to determine the number of columns in the
line.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin: 0in 0in 10pt;" class="MsoNormal"&gt;&lt;span style='line-height: 115%; font-family: "Arial","sans-serif"; font-size: 10pt;'&gt;Peek the next line and determine the number of columns in
the second line. If the sum of the two numbers is less than the expected column
count, the second line is a candidate for merging into the first line. Repeat
this step.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin: 0in 0in 10pt;" class="MsoNormal"&gt;&lt;span style='line-height: 115%; font-family: "Arial","sans-serif"; font-size: 10pt;'&gt;When determining the column type, I first initialize each
column with a narrow type and then widen the column when necessary. I widen the
columns in the following order: bool -&amp;gt; integral -&amp;gt; floating
-&amp;gt;varchar. Because I am not completely sure that I merged the lines
correctly, I relied on the probability rather than the first occurrence to
widen the field. This allows me to run the analysis phase in only one pass. The
drawback is that I do not have 100% confidence on the schema extracted; data
that does not fix the schema would have to be rejected in the import phase. The
analysis phase took about 10 minutes on a fairly slow (in today’s standard)
dual-core Pentium machine.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin: 0in 0in 10pt;" class="MsoNormal"&gt;&lt;span style='line-height: 115%; font-family: "Arial","sans-serif"; font-size: 10pt;'&gt;In the import phase, I did line merging similar to the
analysis phase. The only difference is that, with statistics from the first
phase, I was able to determine whether a short line (i.e. a line that has a
small number of columns) should merge with the previous line or the next line.
The criterion is that the fields split from the merge line have to satisfy the
schema. I imported over 8 million records and had to reject only 1 record. I
visually inspected the reject record and the data is indeed bad. I used SqlBulkCopy to load the data in 1000 record batch.&amp;nbsp;It took about
1 hour and 30 minutes to import the data over the wan into our SQL server in
the cloud.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;p style="margin: 0in 0in 10pt;" class="MsoNormal"&gt;&lt;font size="3" face="Calibri"&gt;In conclusion, rather than determining the table schema
fully deterministically, I employed a little bit of statistics in the process.
I was able to determine the schema faster (with one pass) and higher fidelity
(rejecting bogus data rather than accepting bogus data by widening schema).
After all,&amp;nbsp;statistics is an&amp;nbsp;important part of machine learning today and it allows us
to inject a little bit of intelligence into the ETL process.&lt;/font&gt;&lt;/p&gt;&lt;font size="3" face="Times New Roman"&gt;

&lt;/font&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9701122" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/kcA1d_vRH6c" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/SQL+Server/default.aspx">SQL Server</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2013/01/04/importing-a-large-file-into-sql-server-database-with-machine-learning.aspx</feedburner:origLink></item><item><title>While Lumia 900 is already good, Lumia 920 is even more fluid</title><link>http://feedproxy.google.com/~r/lichen/~3/Sfta7e-1Lco/while-lumia-900-is-already-good-lumia-920-is-even-more-fluid.aspx</link><pubDate>Sun, 25 Nov 2012 19:53:06 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9460998</guid><dc:creator>dotneteer</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=9460998</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2012/11/25/while-lumia-900-is-already-good-lumia-920-is-even-more-fluid.aspx#comments</comments><description>&lt;p&gt;I got my Lumia 900 in April, 2012. A few month later, I was disappointed when the news broke out that Lumia 900 would not be able to upgrade to Windows Phone 8. Luckily, AT&amp;amp;T allows early upgrade to Lumia 920 so that I can continue developing software for latest Windows Phone OS without waiting for my two year commitment to end.&lt;/p&gt;  &lt;p&gt;Lumia 900 was known for excellent software optimization so that the phone felt very fast even with a single core processor. Now Lumia 920, with its Snapdragon S4 processor, feels blazingly fast. The most obvious improvement is acquiring GPS signal; it is almost instant now when I start an app that needs GPS.&amp;#160; &lt;/p&gt;  &lt;p&gt;Apart from being faster and more fluid, I also like the new tile size options that allows me to organize my start screen better. The new phone has a slightly larger display size (4.5” vs. 4.2”) though the physical dimension remains the same. The phone has a larger storage (32GB vs. 16GB) that allows me stored more free Nokia Drive offline maps on my phone so that I can use the GPS without using my monthly data plan. &lt;/p&gt;  &lt;p&gt;So much for my first impression, I will tell more story when I start developing for Windows Phone 8.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9460998" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/Sfta7e-1Lco" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/Windows+Phone/default.aspx">Windows Phone</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2012/11/25/while-lumia-900-is-already-good-lumia-920-is-even-more-fluid.aspx</feedburner:origLink></item><item><title>Implementing set operations in TSQL</title><link>http://feedproxy.google.com/~r/lichen/~3/Vt-seF0bDnA/implementing-set-operations-in-tsql.aspx</link><pubDate>Sun, 11 Nov 2012 02:22:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9361938</guid><dc:creator>dotneteer</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=9361938</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2012/11/11/implementing-set-operations-in-tsql.aspx#comments</comments><description>&lt;p&gt;SQL excels at operating on dataset. In this post, I will discuss how to implement basic set operations in transact SQL (TSQL). The operations that I am going to discuss are union, intersection and complement (subtraction).&lt;/p&gt;
  
&lt;p mce_keep="true"&gt;&amp;nbsp;&lt;/p&gt;
  
&lt;table border="0" cellspacing="0" cellpadding="2"&gt;&lt;tbody&gt;     
&lt;tr&gt;       
&lt;td align="center"&gt;&lt;a href="http://weblogs.asp.net/blogs/lichen/union_2CAFA3FA.png" mce_href="http://weblogs.asp.net/blogs/lichen/union_2CAFA3FA.png"&gt;&lt;img title="union" alt="union" src="http://weblogs.asp.net/blogs/lichen/union_thumb_72C0610D.png" border="0" mce_src="http://weblogs.asp.net/blogs/lichen/union_thumb_72C0610D.png"&gt;&lt;/a&gt; &lt;/td&gt;
        
&lt;td align="center"&gt;&lt;a href="http://weblogs.asp.net/blogs/lichen/intersect_2A26723C.png" mce_href="http://weblogs.asp.net/blogs/lichen/intersect_2A26723C.png"&gt;&lt;img title="intersect" alt="intersect" src="http://weblogs.asp.net/blogs/lichen/intersect_thumb_7A8853AF.png" border="0" mce_src="http://weblogs.asp.net/blogs/lichen/intersect_thumb_7A8853AF.png"&gt;&lt;/a&gt; &lt;/td&gt;
        
&lt;td align="center"&gt;&lt;a href="http://weblogs.asp.net/blogs/lichen/subtract_35DBB96E.png" mce_href="http://weblogs.asp.net/blogs/lichen/subtract_35DBB96E.png"&gt;&lt;img title="subtract" alt="subtract" src="http://weblogs.asp.net/blogs/lichen/subtract_thumb_6047EA8B.png" border="0" mce_src="http://weblogs.asp.net/blogs/lichen/subtract_thumb_6047EA8B.png"&gt;&lt;/a&gt; &lt;/td&gt;
     &lt;/tr&gt;
      
&lt;tr&gt;       
&lt;td align="center"&gt;Union &lt;/td&gt;
        
&lt;td align="center"&gt;Intersection &lt;/td&gt;
        
&lt;td align="center"&gt;Complement (subtraction) &lt;/td&gt;
     &lt;/tr&gt;
   &lt;/tbody&gt;&lt;/table&gt;
  &lt;h3&gt;Implementing set operations using union, intersect and except&lt;/h3&gt;  
&lt;p&gt;We can use TSQL keywords union, intersect and except to implement set operations. Since we are in an election year, I will use voter records of propositions as an example. We create the following table and insert 6 records into the table. &lt;/p&gt;
 &lt;textarea rows="8" cols="100"&gt;declare @votes table (VoterId int, PropId int) 
insert into @votes values (1, 30) 
insert into @votes values (2, 30) 
insert into @votes values (3, 30) 
insert into @votes values (4, 30) 
insert into @votes values (4, 31) 
insert into @votes values (5, 31)&lt;/textarea&gt;   
&lt;p&gt;Voters 1, 2, 3 and 4 voted for proposition 30 and voters 4 and 5 voted for proposition 31.&lt;/p&gt;
  
&lt;p&gt;The following TSQL statement implements union using the union keyword. The union returns voters who voted for either proposition 30 or 31.&lt;/p&gt;
 &lt;textarea rows="3" cols="100"&gt;select VoterId from @votes where PropId = 30 
union 
select VoterId from @votes where PropId = 31&lt;/textarea&gt;   
&lt;p&gt;The following TSQL statement implements intersection using the intersect keyword. The intersection will return voters who voted only for both proposition 30 and 31.&lt;/p&gt;
 &lt;textarea rows="3" cols="100"&gt;select VoterId from @votes where PropId = 30 
intersect 
select VoterId from @votes where PropId = 31&lt;/textarea&gt;   
&lt;p&gt;The following TSQL statement implements complement using the except keyword. The complement will return voters who voted for proposition 30 but not 31.&lt;/p&gt;
 &lt;textarea rows="3" cols="100"&gt;select VoterId from @votes where PropId = 30 
except 
select VoterId from @votes where PropId = 31&lt;/textarea&gt;   &lt;h3&gt;Implementing set operations using join&lt;/h3&gt;  
&lt;p&gt;An alternative way to implement set operation in TSQL is to use full outer join, inner join and left outer join.&lt;/p&gt;
  
&lt;p&gt;The following TSQL statement implements union using full outer join.&lt;/p&gt;
 &lt;textarea rows="3" cols="100"&gt;select Coalesce(A.VoterId, B.VoterId) 
from (select VoterId from @votes where PropId = 30) A 
full outer join (select VoterId from @votes where PropId = 31) B on A.VoterId = B.VoterId&lt;/textarea&gt;   
&lt;p&gt;The following TSQL statement implements intersection using inner join.&lt;/p&gt;
 &lt;textarea rows="3" cols="100"&gt;select Coalesce(A.VoterId, B.VoterId) 
from (select VoterId from @votes where PropId = 30) A 
inner join (select VoterId from @votes where PropId = 31) B on A.VoterId = B.VoterId&lt;/textarea&gt;   
&lt;p&gt;The following TSQL statement implements complement using left outer join.&lt;/p&gt;
 &lt;textarea rows="4" cols="100"&gt;select Coalesce(A.VoterId, B.VoterId) 
from (select VoterId from @votes where PropId = 30) A 
left outer join (select VoterId from @votes where PropId = 31) B on A.VoterId = B.VoterId 
where B.VoterId is null&lt;/textarea&gt;   &lt;h3&gt;Which one to choose?&lt;/h3&gt;  
&lt;p&gt;To choose which technique to use, just keep two things in mind:&lt;/p&gt;
  
&lt;ol&gt;   
&lt;li&gt;The union, intersect and except technique treats an entire record as a member.&lt;/li&gt;
    
&lt;li&gt;The join technique allows the member to be specified in the “on” clause. However, it is necessary to use Coalesce function to project sets on the two sides of the join into a single set.&lt;/li&gt;
 &lt;/ol&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9361938" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/Vt-seF0bDnA" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/SQL+Server/default.aspx">SQL Server</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2012/11/11/implementing-set-operations-in-tsql.aspx</feedburner:origLink></item><item><title>Gave two presentations at LA code camp today</title><link>http://feedproxy.google.com/~r/lichen/~3/7n--YZw0vNE/gave-two-presentations-at-la-code-camp-today.aspx</link><pubDate>Mon, 15 Oct 2012 04:54:55 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:9107007</guid><dc:creator>dotneteer</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=9107007</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2012/10/15/gave-two-presentations-at-la-code-camp-today.aspx#comments</comments><description>&lt;p&gt;I gave two presentation at LA code camp today. The following are the links to the presentation materials:&lt;/p&gt;  &lt;p&gt;1. &lt;a href="https://skydrive.live.com/redir?resid=64D4E06A7777ACA1!323"&gt;Real-time web application development with SignalR&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;2. &lt;a href="https://skydrive.live.com/redir?resid=64D4E06A7777ACA1!733"&gt;A new era of free online education&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=9107007" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/7n--YZw0vNE" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/ASP.NET/default.aspx">ASP.NET</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2012/10/15/gave-two-presentations-at-la-code-camp-today.aspx</feedburner:origLink></item><item><title>Upgrading to Windows 8</title><link>http://feedproxy.google.com/~r/lichen/~3/9LNY8efzPHg/upgrading-to-windows-8.aspx</link><pubDate>Sat, 25 Aug 2012 05:15:56 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8867439</guid><dc:creator>dotneteer</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=8867439</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2012/08/25/upgrading-to-windows-8.aspx#comments</comments><description>&lt;p&gt;As MSDN members can download Windows 8 now, it is time for me to upgrade to Windows 8. Although I upgraded successfully, my journey is a long story. Hopefully my experience can save you some time.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;I had a 120GB SSD that has only 11GB free space left. I am not terribly comfortable with the limited space. So I ordered a 240GB SSD. I used Windows 7 to do an Windows image backup to an external drive. I then booted from the Windows 7 DVD and restored my image to the new drive. It took only 30 minutes to backup, but several hours to restore. After restoring, I went to Computer Management to expand my C drive to use the remaining free space on my new SSD. &lt;/li&gt;    &lt;li&gt;I then tried to download Windows 8 from MSDN. I am running Windows 7 Ultimate. I was not sure which edition of Windows 8 to download. According to &lt;a href="http://en.wikipedia.org/wiki/Windows_8_editions"&gt;this chart&lt;/a&gt;, I can upgrade from Windows 7 Ultimate to Windows 8 pro, but I can only find Windows 8 pro volume licensing edition in MSDN which I am not entitled to download. It turns our that both Windows 8 and Windows 8 pro uses the same media; the activation key decides which edition to activate.&lt;/li&gt;    &lt;li&gt;The Windows 8 upgrader asked me to uninstall Microsoft Security Essential. This is because Windows 8 has Windows Defender built in.&lt;/li&gt;    &lt;li&gt;The Windows 8 user interface is not entirely intuitive. Fortunately, there is a short video from &lt;a href="http://www.youtube.com/watch?v=TuUNzJPgczk&amp;amp;feature=youtu.be"&gt;Mike Halsey&lt;/a&gt; that allows one to get familiar quickly.&lt;/li&gt;    &lt;li&gt;After the upgrade, there is a message asking me to uninstall Virtual Clone Drive. This is because Windows 8 has a built-in feature to mount ISO and VHD files. This is a nice edition.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;So much for my first day of experiments. See you next time.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8867439" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/9LNY8efzPHg" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/Windows+8/default.aspx">Windows 8</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2012/08/25/upgrading-to-windows-8.aspx</feedburner:origLink></item><item><title>Gave a presentation on c# dynamic and dynamic language runtime (DLR) at SoCal .NET User group meeting</title><link>http://feedproxy.google.com/~r/lichen/~3/u_K79DWWZ50/gave-a-presentation-on-c-dynamic-and-dynamic-language-runtime-dlr-at-socal-net-user-group-meeting.aspx</link><pubDate>Fri, 03 Aug 2012 05:21:41 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8811521</guid><dc:creator>dotneteer</dc:creator><slash:comments>2</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=8811521</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2012/08/03/gave-a-presentation-on-c-dynamic-and-dynamic-language-runtime-dlr-at-socal-net-user-group-meeting.aspx#comments</comments><description>&lt;p&gt;You may download my presentation materials &lt;a href="https://skydrive.live.com/redir?resid=64D4E06A7777ACA1!556"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;New to my sample collection is using DynamicObject to construct a dynamic proxy. In contrast to a typical aspect-oriented-programming (AOP) framework with which you can only add cross-cutting concern to methods of an inheritable class or an interface, dynamic proxy does not have this limitation. &lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8811521" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/u_K79DWWZ50" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/lichen/archive/tags/DLR/default.aspx">DLR</category><category domain="http://weblogs.asp.net/lichen/archive/tags/C_2300_/default.aspx">C#</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2012/08/03/gave-a-presentation-on-c-dynamic-and-dynamic-language-runtime-dlr-at-socal-net-user-group-meeting.aspx</feedburner:origLink></item><item><title>Applying algorithms to real-life problem</title><link>http://feedproxy.google.com/~r/lichen/~3/QFCaAcN82Mc/applying-algorithms-to-real-life-problem.aspx</link><pubDate>Sun, 08 Jul 2012 23:19:24 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8711592</guid><dc:creator>dotneteer</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=8711592</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2012/07/09/applying-algorithms-to-real-life-problem.aspx#comments</comments><description>&lt;p&gt;Today I encountered a real-life problem. My wife got some fresh pecan. After we cracked the pecan and took the large pieces, there are some smaller pieces of pecan and shell fragments left in the bowl. My wife asked kids to separate the pecan from the shell. There were three proposals:&lt;/p&gt;  &lt;p&gt;1) Pick the pecan out. The trouble is that we have more pecan pieces than shell pieces. The time for this approach is proportional to number of pecan pieces.&lt;/p&gt;  &lt;p&gt;2) Pick the shell out. It is not clear whether the time would be small because we still need to scan (with our eye) among pecans to find shell. In additional, it is hard to be sure that we are free of shells at the end.&lt;/p&gt;  &lt;p&gt;3) Pour them in water and hope one is heavier than water and the other is lighter. This idea was never tested.&lt;/p&gt;  &lt;p&gt;Then I proposed the 4th approach: grab a few pieces in my palm and apply 1) or 2), which ever way is faster. This works out really well.&lt;/p&gt;  &lt;p&gt;The key is that after I scan the pecan and shells in my palm I never scan the same pieces again. The fast algorithms, whether searching a word in a string, or sorting a list, or finding the quickest time when traveling from point A to point B, are all by finding a smart way to minimize the operations on the same data. &lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8711592" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/QFCaAcN82Mc" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/General/default.aspx">General</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2012/07/09/applying-algorithms-to-real-life-problem.aspx</feedburner:origLink></item><item><title>Is recursion really bad?</title><link>http://feedproxy.google.com/~r/lichen/~3/lvHwtsAPhZQ/is-recursion-really-bad.aspx</link><pubDate>Sun, 08 Jul 2012 22:39:19 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8711437</guid><dc:creator>dotneteer</dc:creator><slash:comments>1</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=8711437</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2012/07/08/is-recursion-really-bad.aspx#comments</comments><description>&lt;p&gt;After my previous post about &lt;a href="http://weblogs.asp.net/lichen/archive/2012/07/05/what-if-you-run-out-of-stack-space-in-c-or-python.aspx"&gt;the stack space&lt;/a&gt;, it appears that there is perception from the feedback that recursion is bad and we should avoid deep recursion. After writing a &lt;a href="http://aspclassiccompiler.codeplex.com/"&gt;compiler&lt;/a&gt;, I know that the modern computer and compiler are complex enough and one cannot automatically assume that a hand crafted code would out-perform the compiler optimization. The only way is to do some prototype to find out.&lt;/p&gt;  &lt;p&gt;So why recursive code may not perform as well? Compilers place frames on a stack. In additional to arguments and local variables, compiles also need to place frame and program pointers on the frame, resulting in overheads.&lt;/p&gt;  &lt;p&gt;So why hand-crafted code may not performance as well? The stack used by a compiler is a simpler data structure and can grow and shrink cleanly. To replace recursion with out own stack, our stack is allocated in the heap that is far more complicated to manage. There could be overhead as well if the compiler needs to mark objects for garbage collection. Compiler also needs to worry about the memory fragmentation.&lt;/p&gt;  &lt;p&gt;Then there is additional complexity: CPUs have registers and multiple levels of cache. Register access is a few times faster than in-CPU cache access and is a few 10s times than on-board memory access. So it is up to the OS and compiler to maximize the use of register and in-CPU cache.&lt;/p&gt;  &lt;p&gt;For my particular problem, I did an experiment to rewrite my c# version of recursive code with a loop and stack approach. So here are the outcomes of the two approaches:&lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="2" width="400"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="133"&gt;&amp;nbsp;&lt;/td&gt;        &lt;td valign="top" width="133"&gt;Recursive call&lt;/td&gt;        &lt;td valign="top" width="133"&gt;Loop and Stack&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Lines of code for the algorithm&lt;/td&gt;        &lt;td valign="top" width="133"&gt;17&lt;/td&gt;        &lt;td valign="top" width="133"&gt;46&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Speed&lt;/td&gt;        &lt;td valign="top" width="133"&gt;Baseline&lt;/td&gt;        &lt;td valign="top" width="133"&gt;3% faster&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="133"&gt;Readability&lt;/td&gt;        &lt;td valign="top" width="133"&gt;Clean&lt;/td&gt;        &lt;td valign="top" width="133"&gt;Far more complex&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;So at the end, I was able to achieve 3% better performance with other drawbacks. My message is never assuming your sophisticated approach would automatically work out better than a simpler approach with a modern computer and compiler. Gage carefully before committing to a more complex approach.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8711437" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/lvHwtsAPhZQ" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/lichen/archive/tags/C_2300_/default.aspx">C#</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2012/07/08/is-recursion-really-bad.aspx</feedburner:origLink></item><item><title>What if we run out of stack space in C# or Python?</title><link>http://feedproxy.google.com/~r/lichen/~3/6VMIfqeShrs/what-if-you-run-out-of-stack-space-in-c-or-python.aspx</link><pubDate>Thu, 05 Jul 2012 05:16:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8698408</guid><dc:creator>dotneteer</dc:creator><slash:comments>2</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=8698408</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2012/07/05/what-if-you-run-out-of-stack-space-in-c-or-python.aspx#comments</comments><description>&lt;p&gt;Supposing we are running a recursive algorithm on a very large data set that requires, say, 1 million recursive calls. Normally, one would solve such a large problem by converting recursion to a loop and a stack, but what if we do not want to or cannot rewrite the algorithm?&lt;/p&gt;  &lt;p&gt;Python has the sys.setrecursionlimit(int) method to set the number of recursions. However, this is only part of the story; the program can still run our of stack space. C# does not have a equivalent method.&lt;/p&gt;  &lt;p&gt;Fortunately, both C# and Python have option to set the stack space when creating a thread. In C#, there is an overloaded constructor for the Thread class that accepts a parameter for the stack size:&lt;/p&gt;  &lt;p&gt;Thread t = new Thread(work, stackSize); &lt;/p&gt;  &lt;p&gt;In Python, we can set the stack size by calling:&lt;/p&gt;  &lt;p&gt;threading.stack_size(67108864)&lt;/p&gt;  &lt;p&gt;We can then run our work under a new thread with increased stack size.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8698408" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/6VMIfqeShrs" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/lichen/archive/tags/C_2300_/default.aspx">C#</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2012/07/05/what-if-you-run-out-of-stack-space-in-c-or-python.aspx</feedburner:origLink></item><item><title>Gave a presentation on SignalR at SoCal Code Camp today</title><link>http://feedproxy.google.com/~r/lichen/~3/cWhD2-3P5Yk/gave-a-presentation-on-signalr-at-socal-code-camp-today.aspx</link><pubDate>Mon, 25 Jun 2012 02:27:21 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:8645666</guid><dc:creator>dotneteer</dc:creator><slash:comments>0</slash:comments><wfw:commentRss>http://weblogs.asp.net/lichen/rsscomments.aspx?PostID=8645666</wfw:commentRss><comments>http://weblogs.asp.net/lichen/archive/2012/06/25/gave-a-presentation-on-signalr-at-socal-code-camp-today.aspx#comments</comments><description>&lt;p&gt;I have a presentation on Real-time web development with SignalR at SoCal Code Camp in the beautiful campus of University of California, San Diego today to a very enthusiastic group of developers. Lots of excellent questions asked. My presentation materials can be &lt;a href="https://skydrive.live.com/redir?resid=64D4E06A7777ACA1!323"&gt;downloaded here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=8645666" width="1" height="1"&gt;&lt;img src="http://feeds.feedburner.com/~r/lichen/~4/cWhD2-3P5Yk" height="1" width="1"/&gt;</description><category domain="http://weblogs.asp.net/lichen/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/lichen/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/lichen/archive/tags/jQuery/default.aspx">jQuery</category><feedburner:origLink>http://weblogs.asp.net/lichen/archive/2012/06/25/gave-a-presentation-on-signalr-at-socal-code-camp-today.aspx</feedburner:origLink></item></channel></rss>
