Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics vs. Array Lists

The system I work on here was written before .net 2.0 and didn't have the benefit of generics. It was eventually updated to 2.0, but none of the code was refactored due to time constraints. There are a number of places where the code uses ArraysLists etc. that store things as objects.

From performance perspective, how important change the code to using generics? I know from a perfomance perspective, boxing and unboxing etc., it is inefficient, but how much of a performance gain will there really be from changing it? Are generics something to use on a go forward basis, or it there enough of a performance change that a conscience effort should be made to update old code?

like image 347
kemiller2002 Avatar asked Sep 18 '08 17:09

kemiller2002


People also ask

Can ArrayList be generic?

Both ArrayList and vector are generic types.

Is ArrayList a generic container?

(ArrayList API documentation) ArrayList is a generic container that gives us array-like convenience for accessing elements (the method .

What is the difference between general version of ArrayList and generic version of ArrayList?

The key difference between them is that ArrayList is not using Generics while ArrayList is a generic ArrayList but they look very similar.

Is it a good idea to use generics in collections?

By using generics, programmers can implement generic algorithms that work on collections of different types, can be customized, and are type safe and easier to read.


4 Answers

Technically the performance of generics is, as you say, better. However, unless performance is hugely important AND you've already optimised in other areas you're likely to get MUCH better improvements by spending your time elsewhere.

I would suggest:

  • use generics going forward.
  • if you have solid unit tests then refactor to generics as you touch code
  • spend other time doing refactorings/measurement that will significantly improve performance (database calls, changing data structures, etc) rather than a few milliseconds here and there.

Of course there's reasons other than performance to change to generics:

  • less error prone, since you have compile-time checking of types
  • more readable, you don't need to cast all over the place and it's obvious what type is stored in a collection
  • if you're using generics going forward, then it's cleaner to use them everywhere
like image 106
Rory Avatar answered Sep 29 '22 11:09

Rory


Here's the results I got from a simple parsing of a string from a 100KB file 100,000 times. The Generic List(Of char) took 612.293 seconds to go 100,000 times through the file. The ArrayList took 2,880.415 seconds to go 100,000 times through the file. This means in this scenario (as your mileage will vary) the Generic List(Of char) is 4.7 times faster.

Here is the code I ran through 100,000 times:

Public Sub Run(ByVal strToProcess As String) Implements IPerfStub.Run
    Dim genList As New ArrayList

    For Each ch As Char In strToProcess.ToCharArray
        genList.Add(ch)
    Next

    Dim dummy As New System.Text.StringBuilder()
    For i As Integer = 0 To genList.Count - 1
        dummy.Append(genList(i))
    Next

End Sub

 Public Sub Run(ByVal strToProcess As String) Implements IPerfStub.Run
     Dim genList As New List(Of Char)

     For Each ch As Char In strToProcess.ToCharArray
         genList.Add(ch)
     Next

     Dim dummy As New System.Text.StringBuilder()
     For i As Integer = 0 To genList.Count - 1
         dummy.Append(genList(i))
     Next
 End Sub
like image 20
torial Avatar answered Sep 29 '22 12:09

torial


The only way to know for sure is to profile your code using a tool like dotTrace.

http://www.jetbrains.com/profiler/

It's possible that the boxing/unboxing is trivial in your particular application and wouldn't be worth refactoring. Going forward, you should still consider using generics due to the compile-time type safety.

like image 32
Ben Hoffstein Avatar answered Sep 29 '22 11:09

Ben Hoffstein


Generics, whether Java or .NET, should be used for design and type safety, not for performance. Autoboxing is different from generics (essentially implicit object to primitive conversions), and as you mentioned, you should NOT use them in place of a primitive if there is to be a lot of arithmetic or other operations which will cause a performance hit from the repeated implicit object creation/destruction.

Overall I would suggest using going forward, and only updating existing code if it needs to be cleaned up for type safety / design purposes, not performance.

like image 21
Peter Avatar answered Sep 29 '22 11:09

Peter