I have been working on several projects. One of the things I have been using for those projects is the Mobile Line of Business Accelerator. In public presentations some developers have asked why when displaying information does the LOB Accelerator end up looping through the data twice. Once for building the data objects and then again to display the newly created data objects. So the last couple of days I have looked at the performance implications of looping twice.
First, I tested how fast I could display the customer list using the current method. A method gets a DataReader, loops through the rows building customer objects and building a generic List. Once a list of customers is created the list is looped through again using a foreach building ListViewItems.
In order to test I used the new System.Diagnostic.Stopwatch. This new Compact Framework 3.5 class provides a high resolution timer. Using Stopwatch can give you 1 millisecond resolution.
perfTimer = Stopwatch.StartNew();
// code being measured
perfTimer.Stop();
long elapsedTime = perfTimer.ElapsedMilliseconds;
In doing testing I looped through 100 records a 101 times. I dumped the first result and got an average time of 546 milliseconds. Not bad performance.
Next, I directly read the rows and add them to the ListView. In doing testing I found it took an average of 546 milliseconds. What the heck is going on?
It turns out that objects and looping in memory is very efficient. To see any difference would take better resolution than a millisecond. So the key take away is that creating strongly typed objects is very performant even if you have to do an extra loop.
Comments