Archive for September, 2007

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

Comments 4 Comments »

I have Vista installed for … 6 months now. Normally around this period XP would have been a rotten corpse because of the heavy usage and frequent software installs/uninstalls. I cannot say that Vista was braver. I encountered until now two situations that were so hairy that I was at a feet from reinstalling the beast. Still I remembered on NICE feature of Vista: System Restore.

How it works?

Everytime you install a piece of software or unninstall one, or important changes are made to the system, Vista makes a backup of important system files (notable here are registry and other important files). This backup is stored and given a time stamp and relevant information about the reason for it’s creation. Unfortunately this consumes space (about 300mb per backup) but if you are carefull, you can select only relevant ones and delete the ones not deemed worthy. And of course one can create a restore point at any given time nomatter the status or reason.

Ok, ok, how does this help me?! Obviously, you will find yourself in the devious situation where the OS is behaving like a madman. What do you do? If you are a masterfull sorcerror and knowleadge-able in the inner workings of Windows you can repair it, using the dark arts and a lot of googling :)

Or, you can restore the system to a previous state, which you know it works. Yes, you might lose some stuff, but only information from the system state, all your data is still there.

There it is, now you can rest a little bit more easy, Vista gives you the tools needed for its survival and for keeping your hair from going gray…

Cheers

Comments 1 Comment »