Generics vs. Array Lists

后端 未结 12 1485
孤独总比滥情好
孤独总比滥情好 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:47

    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.

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

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

    What does autoboxing/unboxing have to do with generics? This is just a type-safety issue. With a non-generic collection, you are required to explicitly cast back to an object's actual type. With generics, you can skip this step. I don't think there is a performance difference one way or the other.

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

    The biggest gains, you will find in Maintenance phases. Generics are much easier to deal with and update, without having to deal with conversion and casting issues. If this is code that you continually visit, then by all means take the effort. If this is code that hasn't been touched in years, I wouldn't really bother.

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

    If the entities in the ArrayLists are Object types, you'll gain a little from not casting them to the correct type. If they're Value types (structs or primitives like Int32), then the boxing/unboxing process adds a lot of overhead, and Generic collections should be much faster.

    Here's an MSDN article on the subject

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

    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.

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