I've been asked to work in an enterprise application that is using ArrayList everywhere (a Find in Files search came back with 1500+ instances of the keyword ArrayList). Very large objects are being passed around in very long ArrayLists to various methods, in many cases these ArrayLists are even 3 dimensional. I already know first hand how terrible it is to work inside this code, however, I am not too sure about the performance impact this is having on the application. For example, when the objects are retrieved from the list they have to be explicitly cast from object, versus if a Generic List had been used no explicit casting is done. To test the performance I wrote a very simple test case shown below:
AVeryLargeObject largeObj = new AVeryLargeObject();
int iterations = int.Parse(textBox1.Text);
timers.SetStart("ArrayListAdd"); // Using Win32PerformanceTimer
ArrayList oldList = new ArrayList(iterations);
for (int i = 0; i < iterations; i++)
{
oldList.Add(largeObj);
}
timers.SetEnd("ArrayListAdd");
timers.SetStart("ArrayListPull");
for (int i = 0; i < iterations; i++)
{
AVeryLargeObject obj = (AVeryLargeObject)oldList[i];
}
timers.SetEnd("ArrayListPull");
timers.SetStart("GenericListAdd");
List<AVeryLargeObject> properList = new List<AVeryLargeObject>(iterations);
for (int i = 0; i < iterations; i++)
{
properList.Add(largeObj);
}
timers.SetEnd("GenericListAdd");
timers.SetStart("GenericListPull");
for (int i = 0; i < iterations; i++)
{
AVeryLargeObject obj = properList[i];
}
timers.SetEnd("GenericListPull");
While I definitely see slower performance when pulling objects and casting from ArrayList I had to run upwards of a million iterations to see a millisecond difference. Does this mean the performance between these 2 isn't really that different? It seems the ArrayList pull may be as much as twice as slow but this code runs so fast that I wonder if refactoring this application to use Generic List would even yield a performance boost? Would this performance be affected by the size of the objects being stored and whether the object's properties are populated? What about memory usage?