Generics vs. Array Lists

后端 未结 12 1484
孤独总比滥情好
孤独总比滥情好 2020-12-10 12:19

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

相关标签:
12条回答
  • 2020-12-10 12:37

    Using generics should also mean that your code will be simplier and easy to use if you want to leverage things like linq in the later c# versions.

    0 讨论(0)
  • 2020-12-10 12:38

    It depends, the best answer is to profile your code and see. I like AQTime but a number of packages exist for this.

    In general, if an ArrayList is being used a LOT it may be worth switching it to a generic version. Really though, it's most likely that you wouldn't even be able to measure the performance difference. Boxing and unboxing are extra steps but modern computers are so fast that it makes almost no difference. As an ArrayList is really just an normal array with a nice wrapper, you would probably see much more performance gained from better data structure selection (ArrayList.Remove is O(n)!) than with the conversion to generics.

    Edit: Outlaw Programmer has a good point, you will still be boxing and unboxing with generics, it just happens implicitly. All the code around checking for exceptions and nulls from casting and "is/as" keywords would help a bit though.

    0 讨论(0)
  • 2020-12-10 12:41

    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
    
    0 讨论(0)
  • 2020-12-10 12:46

    My old company actually considered this problem. The approach we took was: if it's easy to refactor, do it; if not (i.e. it will touch too many classes), leave it for a later time. It really depends on whether or not you have the time to do it, or whether there are more important items to be coding (i.e. features you should be implementing for clients).

    Then again, if you're not working on something for a client, go ahead and spend time refactoring. It'll improve readability of the code for yourself.

    0 讨论(0)
  • 2020-12-10 12:46

    Depends on how much is out there in your code. If you binding or display large lists in the UI, you would probably see a great gain in performance.

    If your ArrayList are just sprinkled about here and there, then it probably wouldn't be a big deal to just get it cleaned up, but also wouldn't impact overall performance very much.

    If you are using a lot a ArrayLists throughout your code and it would be a big untertaking to replace them (something that may impact your schedules), then you could adopt a if-you-touch-it-change-it approach.

    Main thing is, though, that Generics are a lot easier to read, and are more stable across the app due to the strong typing you get from them. You'll see gains not just from performance, but from code maintainablity and stability. If you can do it quickly, I'd say do it.

    If you can get buy-in from the Product Owner, I'd recommend getting it cleaned up. You love your code more afterward.

    0 讨论(0)
  • 2020-12-10 12:46

    Generics has much better performance especially if you'll be using value-type (int, bool, struct etc.) where you'll gain a noticeble performance gain.

    1. Using Arraylist with value-types causes boxing/unboxing which if done several hundred times is substantialy slower then using generic List.

    2. when storing value-types as object you'll up to four time memory per item. While this amount won't drain your RAM the cache memory that is smaller could contain less items, Which means that while iterating a long collections there would be many copies from the main memory to the cache that would slow your application.

    I wrote about here.

    0 讨论(0)
提交回复
热议问题