I was busy these days optimizing code for speed. What I found is no less than mind boggling. Using the right constructs one can speed up pieces of code tremendeously (read this like: a code that runs 10 minutes can be optimized sometimes to run in 10 seconds).
Tips:
Try to get data from the database in a single run. This is an old one but it seems people tend to forget this.
Carefull with the binding. With the advent of persistance frameworks, getting data from the database and binding it to controls became easy. Unfortunately due to design sometimes this gets in our way. For example, we have a business object called Person and which has a property homeadress of type Adress. In normal frameworks, the homeadress object is loaded only when it’s needed. So, we get a list of persons and bind this to a grid. And we say that one of the columns is actually a property of adress (person.homeadress.streetname). During binding, for each person, a call is made to the database to load the homeadress object and retrieve it’s streetname property. Thus for a 10 persons list we have 11 calls to the database (1 for retrieving the list of persons and 1 for each person to get the adress object). Try to avoid this !!! Use simple ADO objects of modified business objects instead.
Avoid find using predicates on lists. Searching using predicates is very nice and somehow natural but for large lists (1000+), the search time is high. Try using dictionaries for fast retrieval of elements.
Avoid List<T> and use Dictionary<T,Y> when making caches. This is an odd one, i did not digg deep into this problem but it seems that when creating a cache (parse a list, add the items) and afterwards accessing elements, for the same amount of data and the same logic, a dictionary was 10 times faster.
Use explicit casting when possible. The most common scenario is when we have a DataReader and we try to read data from it. If we know the first column is a string we could do it like datareader[0].toString() or (string)datareader[0]. And now the milion dolar question, what’s faster … take a wild guess
. Also, don’t do int.Parse(datareader[0].toString()) if you know there’s an int, use (int)datareader[0].
… more to come here



Entries (RSS)
October 1st, 2007 at 9:37 am
Interesting points you have here about optimizations, however I have something to add regarding your affirmations about the bindings. Modern ORM frameworks should have the option to use lazy loading or not, in fact nhibernate’s default I think is not to use lazy loading. This means that the mapped entities are loaded all at once. When you use lazy loading, the idea would be to use it only in situations when the data is not accessed very often, otherwise you get the results you described.
October 4th, 2007 at 12:03 pm
>>Carefull with the binding.
I agreed with Michai for binding statement. In our project, we try to not use the direct ADO object or DataReader or DS .. we use only business objects in Presentation layer…
>>Avoid List and use Dictionary when making caches.
I think you need to explain about that more details. (how did you know Dictionary class is 10 times faster than List? how did you test? )
seems like “title” is more interesting than the content.
Thanks.
October 4th, 2007 at 7:19 pm
I didn’t know about the casting issue, that’s interesting. Thanks for the information.
October 12th, 2007 at 9:57 pm
Hello
Very much for a long time searched for article on this theme.Thanks.
by