Optimizing Applications…

I recently had an interesting experience doing some optimization with a .Net application. The portion I was interesting in speeding up was fairly simple: A ListView that is populated with a list of objects that are returned from a database. (Note: The list of things being returned was actually a list of objects – not just a DataSet.) The basic psuedo-code went something like this:


get list of things from database
for each thing in a list of things
do a database look-up for two properties on the thing
create a new ListViewItem
add the ListViewItem to the ListView

The performance stats were as follows:
Time to add 740 items to list: 1493ms
Time to add 4721 items to list: 8919ms

(The odd number of items is just because that’s what I had in my test database at the time. It seemed like a suitable set of data to work with).

The roughly 1.5 seconds to load the list of objects was bearable. The almost 9 seconds to load the list of 4700 objects – not so much. It gave the appearance that the app had hung. The first thing that struck me is how the application itself was slow to re-draw the ListView, so the first pass I switched it around to:


get list of things from the database
for each thing in the list of things
do a database look-up for two properties on the thing
create a new ListViewItem
add new ListViewItem to a list of ListViewItems

Add all the ListViewItems at the same time

After this first pass of rudimentary optimizing things, the stats were as follows:
Time to add 740 items to list:679ms
Time to add 4721 items to list: 5034ms

A decent improvement, but there’s definitely still some room for improvement. I then switched things around and used a pair of dictionaries to save some database look-ups:


create a pair of dictionaries
get list of things from the database
If property 1 is in the dictionary
grab the property from the dictionary
else
do a database look-up for the property. Add the property to the dictionary.
... do the same for property 2...
add new ListViewItem to a list of ListViewItems

Add all the ListViewItems at the same time

How did the numbers stack up using the dictionary?

Time to add 740 items to list: 214ms
Time to add 4721 items to list: 1272ms

So we went from originally taking almost a second and a half to load a small list of items down to 2/10ths of a second – a 700% increase in performance. Not too shabby.

So what lessons are there to be learned here? It’s pretty much the same with any sort of optimizations in programming:

  1. The fastest code to run is no code at all. If it doesn’t need to be run, don’t run it.
  2. If you can cache it and look it up faster that way, do it.
  3. If you can’t speed it up, at least make it look faster. Hide longer loading times under animated loading screens (if already present), behind animations and that sort of thing.

I recently heard something interesting mentioned by a rather famous game developer (if there is such a thing!). He briefly mentioned loading game data while the intro movie to the game was playing. That way, when the player was done watching the short movie clip, that portion of the game was ready to be played. Again, if you can’t make it load faster, at least make it look like it is loading faster!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s