0

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?

Padrino
  • 1
  • 1
  • Just be careful in your timed testing that the unused object (both in the `cast` and in the `assign` loops) aren't just optimized away. [Casting is relatively cheap](https://stackoverflow.com/q/1923882/314291), and since you are using 'large objects' they are already on the heap. IMO the main benefit for moving to strongly typed generics would be for type safety, not for performance or memory reasons. Can I suggest ensuring the Unit test suite is up to date and then iterative cycles of refactoring? – StuartLC Nov 09 '17 at 05:43
  • You've already done the the sensible thing and tested the performance. If the numbers you mention are on par with the live application you're good. Also remember that sometives expensive refactoring can be replaced by adding way cheaper hardware. Compare the cost of 16GB more RAM to the cost of you refactoring code for a week. – Markus Deibel Nov 09 '17 at 06:16
  • Possible duplicate of [ArrayList vs List<> in C#](https://stackoverflow.com/questions/2309694/arraylist-vs-list-in-c-sharp) – Ext3h Nov 09 '17 at 06:50

0 Answers0