Saturday, April 30, 2011

Where can I find a practical/real time scenario of using `List<T>`?

Where can I find a practical/real time scenario of using List<T>?
I want to know where and how we can implement List<T>.

Any Code example, or any link will do.

From stackoverflow
  • How about the MSDN page on List<T>? There's an explanation and an example there.

  • Simply put, whenever you want a list of something (list of number, list of persons, list of whatever), you just use a List, List, List. Maybe I missed the point of your question.. you ask how to implement List? Or how to implement code using a list? I guess it's more the second choice.

    Cerebrus : +1 for anyone who recommends lmgtfy! ;-)
  • In a 'real-time' system, you'll likely want to make sure your list is created large enough to hold all the data you intend to put in it when you first create it (use the constructor where you pass a capacity).

    You'll also want to avoid the following operations (and maybe more), as they are relatively expensive (ie. they may look O(1), but aren't):

    • Insert(int, T)
    • Remove(T)
    • RemoveAt(int)

    There are other O(N) operations on a list, but most of those you'd guess by looking at what the method does.

    That said, the only way to know if it'll work for your performance requirements is to test it. Get yourself a good test harness and pound it to make sure it means your needs.

  • Sounds like you will need to model the performance needs of your service to see if List or any data structure will work. Do you need millions of items per second? Billions? Milli-seconds? Make sure you can deliver to your specifications, or re-write the specifications!

  • I have never written software for realtime-like systems, and I doubt that .NET is a good candidate for it. You never know when the garbage collector will trigger, pause your programs for an unknown amount of time before it returns the execution to your program like nothing happened.

    Could you tell us more about the type of data, and the type of operations you expect to do with the list? The slowest part with a List is probably the resize of the underlying array that occurs when you add more items that it was originally created for. You can get away with this by using the constructor that accepts an integer as the initial size, if you can calculate that information. Also, stay away from some of the methods like Insert/Remove, they aint as fast as they sound like.

    Peter Lillevold : How can you say that .Net cannot be used with realtime systems if you have no experience in building such beasts?
    Simon Svensson : @Peter, That's why I used the wording "and I _doubt_ that". It is required, to my knowledge, that a realtime system should execute before a preconfigured time. This is not something that can be guaranteed if the code can be interrupted at any time by the garbage collector, paused for any time, and resumed later without any notification.
    Peter Lillevold : True, I pull back my -1. The .Net runtime can be configured to run GC on a separate threads with multicore processors, though, which will make it more likely to work in realtime scenarios.
    Simon Svensson : There are more information at http://blogs.msdn.com/maoni/archive/2004/09/25/234273.aspx about different garbage collections. While all pause the execution in some what, some does it for shorter intervals and are optimized for high responsiveness.
    1. Identify alternatives to List<>
    2. Measure performance of List<> compared to alternatives for your specific case. General answers to your question cannot be accurate.
  • What kind of real time is real time? Because if you want to do REAL real time, neither .Net nor Windows is your friend.

    And it ain't simple: http://www.eurotech-inc.com/info/white-papers/Managed_Code_for_Real_Time.pdf

  • For performance-critical systems, I agree that List<T> might not be appropriate for the job because it's a little bloated in terms of implementation. Sure it's easy to use, but it has a lot of caveats in it.

    To really find out how it performs:

    • Create a program that does some typical operations for your application then profile it using List<T>.
    • Study the internal workings of List<T> using RedGates reflector to really, really find the bottlenecks and find alternative implementations for it. For example, one unexpected slowdown point would be accessing the Count() method, as it iterates through all the contents of the list.
    • Your finds should be basis enough to implement a "turbofied" implementation of the IList<T> interface that you can use on your system with reasonable performance improvements.
  • I think you are rather looking for some comparison of List with other data structures in C#. It would be nice if you filter down your question.

0 comments:

Post a Comment