Just to be clear the case applies not only to head inserts this is
just the worst case ... any large list with lots of inserts and
removes will be more performant as a LinkedList<T> as opposed to a
List<T>.

On 7/4/07, Greg Young <[EMAIL PROTECTED]> wrote:
Just to add to everyone else's commentary you should in some
circumstances also prefer LinkedList<T> over List<T> for performance
reasons (in particular if you are adding and removing alot of head
nodes from a large list as this is List<T>'s worst case (due to large
copies of arrays internally). As an example:

    class Foo
    {
        public int i;
        public string s;

        public Foo(int _i, string _s)
        {
            i = _i;
            s = _s;
        }
    }

        static void Main(string[] args)
        {
            int Iterations = 100000;
            List<Foo> ListFoo = new List<Foo>();
            LinkedList<Foo> LinkedListFoo = new LinkedList<Foo>();
            Foo f;
            for (int i = 0; i < 10000; i++)
            {
                f = new Foo(i, i.ToString());
                ListFoo.Add(f);
                LinkedListFoo.AddLast(f);
            }
            Stopwatch s = new Stopwatch();
            f = new Foo(12, "test");
            Console.WriteLine("Starting test on List<T>");
            s.Start();
            for (int i = 0; i < Iterations; i++)
            {
                ListFoo.RemoveAt(0);
                ListFoo.Insert(0, f);
            }
            s.Stop();
            Console.WriteLine(Iterations + " iterations took " + s.Elapsed);
            Console.WriteLine("Starting test on List<T>");
            s.Reset();
            s.Start();
            for (int i = 0; i < Iterations; i++)
            {
                LinkedListFoo.RemoveFirst();
                LinkedListFoo.AddFirst(f);
            }
            s.Stop();
            Console.WriteLine(Iterations + " iterations took " + s.Elapsed);
        }


output:
Starting test on List<T>
100000 iterations took 00:00:02.3082447
Starting test on List<T>
100000 iterations took 00:00:00.0173658

Cheers,

Greg

On 7/4/07, Martín Salías <[EMAIL PROTECTED]> wrote:
> Hi, Russell.
>
> Beside what the rest said, I would advise you to prefer a generic collection
> always, as the non-generic once can be slowly phased out as versions pass.
> Indeed, the .NET framework for Silverlight doesn't have them available
> anymore. Generics are also better type-safe and performance wise, and lead
> to much cleaner code in general in my opinion.
>
> Regards,
> --
> Martín Salías
> www.Salias.com.ar
> Agile Alliance Member - Microsoft MVP
>
>
> On 7/3/07, Russell Collins <[EMAIL PROTECTED]> wrote:
> >
> > I am trying to determine if I should use a generic List<T> or a
> > HashTable to hold some data objects.  The main thing is that I want to
> > know if there is general knowledge that one just performs better than
> > the other.  Any feedback would be greatly appreciated.
> >
> > ===================================
> > This list is hosted by DevelopMentor(r)  http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> > http://discuss.develop.com
> >
>
> ===================================
> This list is hosted by DevelopMentor(r)  http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>


--
Studying for the Turing test



--
Studying for the Turing test

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to