+(91) 914 925 3855 info@infinityprosoft.com House No. 0, Meerpur, Meerut

The Wonderful World of Asp-Net Generic List


The Generic List

This little baby has given me more trouble, performance gain and everything else imaginable and because of this I thought I would sing its praises this to you this afternoon in a series of articles. Let's take this puppy for a ride! Create a test page and add the following to the using block; using System.Collections.Generic; and then let's stuff our list with some data.

using System; using System.Data; using System.Configuration; using System.Collections; using System.Collections.Generic; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;

public partial class list_t: System.Web.UI.Page

protected void Page_Load (object sender, EventArgs e)

List l = new List (); for (int i = 0; i <100000; i ++)

l.Add (i.ToString ());

Response.Write (l.Count.ToString ());

As you can see we have a list of 100 thousand strings. Simple enough right? OK let's try to find a value in that list and measure some performance. First we will turn on our Asp.Net trace in the .aspx page and run some basic code to look for values ​​in our list.

List l = new List (); for (int i = 0; i <100000; i ++)

l.Add (i.ToString ());

Trace.Warn ("Contains", "Starting"); if (l.Contains ("49999")) Response.Write ("Yes"); else Response.Write ("No"); Trace.Warn ("Contains", "Ended");

Trace.Warn ("Contains", "Starting"); if (l.Contains ("95842584147")) Response.Write ("Yes"); else Response.Write ("No"); Trace.Warn ("Contains", "Ended");

On my machine I am finding, or not finding, that value in between 0.001270 and 0.002004 which is not bad. What's a couple of milliseconds between friends? But let's see if we can speed things up a bit and to do that we need to sort our list and try again using the binary search method of our list.

l.Sort ();

Trace.Warn ("BinarySearch", "Starting"); if (l.BinarySearch ("99799")> -1) Response.Write ("Yes"); else Response.Write ("No"); Trace.Warn ("BinarySearch", "Ended"); Ah now by sorting our list and using the binary search I have just scanned a 100 thousand row record set in about 60 uSecs: 0.000068 in the application side. Not bad for an old guy huh?

Now I know what you're asking … What does this all mean? Well, say goodbye to all of those bloated ADO.net methods! Stick with me in my next additions to the List and I will explain why I have come to that conclusion.

For now … Happy coding!


Source by John Belthoff