Monday, March 26, 2012

Performance: MSSQL vs Generics

Hi everybody,
I've been wondering, which option is best performant, the WHERE clause or in the case of a generic list the FindAll() method?
Thing is, I got a huge List<T> (around 50,000 rows, obviouslycached), for all I know implementing IComparer<T> to sort thelist is faster than doing another query (with the ORDER BY clause), sothat's how I'm doing it now but I'm not 100% sure.
Has anyone done tests on this?, any tips/advice?
ThanksHello.
the only way you have to know for sure is to measure it...btw, are you sure that using the icomparer is faster? what type of sorting algorythm are you using? i though that bds would be able to perform those operations faster but i may be wrong...

1. List.FindAll takes a Predicate(a type of delagate that takes the type of the list item and returns bool) which means that there is a LINEAR iteration and the method of the predicate is applied to each item. For zillion items zillian executions of the method.
http://msdn2.microsoft.com/en-us/library/fh1w7y8z(en-us,vs.80).aspx
Now this method can be as simple as a null check and as long as a million lines of code. So you decide.
So in many situations you may be able to do a lot more with a predicate than with a SQL query.
2. However your question implied that it can be done either way. A db can have fancier indexing and beat iterations any time but especially in larger collections. You do have the db hit to pay for up front though, I think a small price to pay.
3. There is a nice library http://www.wintellect.com/powercollections/ which is aimed exactly at closing this gap. Many collections use a red-black tree search instead of linear. Also they provide some nice props like union, intersection etc. and a bunch of canned algorithms.
HTH!


Luis Abreu wrote:

Hello.
the only way you have to know for sure is to measure it...btw, are you sure that using the icomparer is faster? what type of sorting algorythm are you using? i though that bds would be able to perform those operations faster but i may be wrong...


The IComparer interface allow comparison for types that cannot be compared which lets you do BinarySearch( ). I don't think a List will be faster than RDBMS(relational database management system) SELECT and INNER JOIN.

0 comments:

Post a Comment