#Google Analytic Tracker

Pages

Aug 4, 2009

Did you increase or decrease your application performance using IEnumerable in Linq (Part 1)?

I love Linq, there is no doubt about that. I pretty much use Linq whenever is appropriate. Linq simply makes my code much more readable and easier to understand. Another reason is the power the extension methods the Linq library provide for IEnumerable<T> object.

In an older post, I mentioned a bit about IEnumerable and how it works. This time, I am going to talk a bit more about how you can use IEnumerable to help improve your application.

Good Performance using IEnumerable?

Consider the following code:

private bool IsToyotaTheHighestSale2(List<Vehicle> totalSaleList)
{
    int toyotaSale =  (from v in totalSaleList
          where v.Make == Make.Toyota
          select v).Count();

    int hondaSale = (from v in totalSaleList
          where v.Make == Make.Honda
          select v).Count();

    int bmwSale = (from v in totalSaleList
          where v.Make == Make.BMW
          select v).Count();

    //Compare the result
    if (toyotaSale > hondaSale)
        if (toyotaSale > bmwSale)
            return true;

    return false;
}

In the above example, we use Linq to get a count of all the car sales, follow by compare toyta sale with honda and bmw sale.

Now, let check the following:

private bool IsToyotaTheHighestSale2(List<Vehicle> totalSaleList)
{

    IEnumerable<Vehicle> toyota 
        = from v in totalSaleList
          where v.Make == Make.Toyota
          select v;

    IEnumerable<Vehicle> honda 
        = from v in totalSaleList
          where v.Make == Make.Honda
          select v;

    IEnumerable<Vehicle> bmw 
        = from v in totalSaleList
          where v.Make == Make.BMW
          select v;

    //Count all the list first
    int toyotaSale = toyota.Count();

    //Compare the result
    if (toyotaSale > honda.Count())
        if (toyotaSale > bmw.Count())
            return true;

    return false;
}

Comparing IsToyotaTheHighestSale() with IsToyotaTheHighestSale2(), which one would perform faster? The answer is the IsToyotaTheHighestSale2().

Why? When you use Linq and get an IEnumerable there is actually no calculation, until you act on the IEnumerable. In IsToyotaTheHighestSale2(), if toyota sale is not higher than honda sale, the function does not need to calculate bmw sale. This is why the latter function run faster in general case.

Of course, above is only a very simple example. Imagine that you need to execute a function tens of thousands of times, and your Linq statement is a lot more complicated than the above example. Acting on the Enumberable later in the logic would save you running unnecessary code.

Conclusion

You can improve your application performance by acting on the IEnumerable object right when you need it, instead of getting the result in the early stage of your logic.

However, there are another cases when you should not use IEnumerable, which I will cover in the next blog.

Aug 3, 2009

PreCode Setup Testing

This blog is merely my PreCode setup test. It took me a long time to setup the PreCode because I did realized the preview function on Blogger, and the Live Writer Preview does not execute JavaScript. I kept wondering why the code setup doesn’t work until I actually try to publish a post.

Here are two examples on how the Syntax Highighter works:

JavaScript Example:

 function doNothing()
 {
   var a = 1;
   var b = 2;
   var nothing = a*b;
   // alert(nothing);
 }

C# Example:

public static string GetHelloMessage()
{
    var a = "Hello";
    var b = "World";
    
    var someArray = (from p in SomeArray
                     where p is Dat
                     select p).ToArray();

    return string.Format("{0} {1}", a, b);
}

What is PreCode

Precode is a code snippet highlighter plug-in for Windows Live Writer. It uses an open source JavaScript syntax highlighter that format your code in HTML.

This plug-in works by adding a tag <pre> for the code that you want to insert.

http://www.codeplex.com/precode

Screenshot:

image

After you insert your code, you will end up with something like the following:

<pre class="brush: csharp; gutter: false; toolbar: false; smart-tabs: false;">public static string GetHelloMessage()
{
    var a = &quot;Hello&quot;;
    var b = &quot;World&quot;;
    
    var someArray = (from p in SomeArray
                     where p is Dat
                     select p).ToArray();

    return string.Format(&quot;{0} {1}&quot;, a, b);
}</pre>

Since Precode uses Javascript, when you add test your setup in Windows Live Writer, or Blogger, it won’t show the formatted text. You will have to publish your blog to see if it works or not.

Note: I couldn’t get the PreCode to work properly in WindowsXP. The “Surround With” dropdown appears behind the main form. Maybe this is a WPF issue, but who knows, unless someone look into this.

How to set your Blogger to use PreCode

There is two parts:

1. Download and install PreCode

2. Setup your Syntax Highlighter

It took me awhile to get the JavaScript right. I am not an expert in JavaScript. However, this is what I did:

In the blogger settings, goto Layout Tab –> Edit HTML. In your Edit Template, add the following code in your html <head> section and save your template:

<script language='JavaScript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'>
</script>
<link href='http://alexgorbatchev.com/pub/sh/2.0.320/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/2.0.320/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shCore.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushCSharp.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushXml.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushAS3.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushJScript.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushCss.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushCpp.js'/>
<script language='javascript' src='http://alexgorbatchev.com/pub/sh/2.0.320/scripts/shBrushJava.js'/>

<script language='javascript'>
$(document).ready(function () {
  $(&quot;pre br&quot;).after(&quot;\n&quot;).remove();
  SyntaxHighlighter.config.clipboardSwf = &#39;http://alexgorbatchev.com/pub/sh/2.0.320/scripts/clipboard.swf&#39;;
  SyntaxHighlighter.all();
});
</script>

Notice that in the above Javascript, I am referencing the js files from alexgorbatchev.com domain. You can definitely put these js files somewhere else. In fact, you can modify these javascript files to have additional keywords.  In the above example, I didn’t include all the available syntax highlighters. Go to the following link for the others.

http://alexgorbatchev.com/wiki/SyntaxHighlighter:Brushes 

References

Thanks to the following links, I was able to set up my blog using Precode:

http://www.codeplex.com/precode

http://ersinbasaran.blogspot.com/2009/07/code-syntax-highligthting-on-bloggercom.html

Jul 2, 2009

More Windows 7 Experience

Wow, I haven’t write any useful blog for awhile. Originally, I was thinking about writing about Linq. However, I realized there are too many Linq related article out there.

Anyways, after using Windows 7 RC for quite some time on my Mac, one major positive thing I notice is the compiling speed of my work (VS) solution, compare with XP. Of course, XP limited you 2GB of addressable memory. Even with the 3GB hack in the boot.ini, which for some reasons killed my video on my new Core Duo 2.8GHz with 3GB machine from Dell, my Mac 2.4GHz with 2GB with Windows 7 still compile a lot faster. :)

Issues:

Well, so far, I encounter 2 freezes, with no blue screen, and I can’t find any system error message in the log file.

And here is the latest issue I found, see if you can see the problem below

image

I found that funny, notice the start menu is off.