In my previous post, I have said that using LINQ query instead of SortedDictionary not only could be useful , elegant but also less processor intensive. In this post, I will show you a real comparison between the same the method but one with SortedDictionary and other with LINQ orderby query. I wrote a simple console application that mimics the action of GetSignature in LINQ.Flickr. Here, I will focus only on the sorting part that's why I removed the hashing and initialization of the method.
So, lets assume there are 10 url pairs that we need to sort for getting the signature. So, we need to create an array of 8 items and the processing method inserts another 2 (api_key and method name). Finally, when I run the two routines and took a snapshot from jetbrain's dottrace , I saw the following
Now, you can see that there are two methods TestSortedItemsWithSortedDic(SortedDictionary) and TestSortedItems (LINQ). The difference is almost 3 times the LINQ orderby query. Expanding the TestSortedItemsWithSortedDic will reveal the following
As you can see that adding items into sorted dictionary takes around 6.37% of the CPU cycle which is definitely because of the sort algorithm that it runs on each add. Also, there is another 1.39% during the initialization. Only these two items take up 7.76%, let alone the other SortedDictionary entries. Now, lets see how the processing with LINQ orderby looks like
LINQ iteration takes up 1.77%(where the original execution take place) , Dictionary.Add takes 0.54% comparing to 6.37% with sorted dictionary and additional orderby and select take 0.17 and 0.14% respectively. So, in total add and sorting takes up to (1.77 + 0.54 + 0.17 + 0.14) 3.39% of CPU cycles. Here to include that, highest CPU index may vary with current load, but the ratio remains almost the same.
Therefore, it is almost clear that we can replace SortedDictionary with LINQ and without it .stripped down version of .net in Silverlight is not a bad idea :-). Also, those who commented on my last post for live comparison thank you !!. You can also download the test code I have used here and run it in dottrace to see it live (I have used dottrace 3.1).