Exemple #1
0
        private string insertInHeadObjects(ArrayList al)
        {
            string winner;
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            int size = 10000000;

            for (int i = 0; i < size; i++)
            {
                ExampleClass ec = new ExampleClass();
                al.Add(ec);
            }

            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;

            Console.WriteLine("\n\n=================================================================================");
            Console.WriteLine("Time to insert {0} objects in ArrayList: {1}", size, ts);

            stopWatch.Reset();
            stopWatch.Start();
            LinkedList<Object> ll = new LinkedList<Object>();

            for (int i = 0; i < size; i++)
            {
                ExampleClass ec2 = new ExampleClass();
                ll.AddFirst(ec2);
            }

            stopWatch.Stop();
            TimeSpan ts2 = stopWatch.Elapsed;

            Console.WriteLine("Time to insert {0} objects in LinkedList: {1}", size, ts2);

            if (ts > ts2)
            {
                Console.WriteLine("\tLinkedList is quicker!");
                return winner = "LinkedList";
            }
            else
            {
                Console.WriteLine("\tArrayList is quicker!");
                return winner = "ArrayList";
            }
        }
Exemple #2
0
        private string insertObjectsInMiddle(ArrayList al)
        {
            string winner;
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            int size = 1000;

            for (int i = 0; i < size; i++)
            {
                ExampleClass ec = new ExampleClass();
                al.Add(ec);
            }

            int size2 = 100;
            for (int i = 0; i < size2; i++)
            {
                al.Insert(size / 2, i);
            }

            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;

            Console.WriteLine("\n\n=================================================================================");
            Console.WriteLine("Time to insert {0} objects in the middle of ArrayList: {1}", size, ts);

            stopWatch.Reset();
            stopWatch.Start();

            LinkedList<Object> ll = new LinkedList<Object>();
            ExampleClass ec2 = new ExampleClass();

            for (int i = 0; i < size; i++)
            {
                ll.AddFirst(ec2);
            }

            LinkedListNode<object> node = ll.Find(ec2);

            for (int i = 0; i < size2; i++)
            {
                ll.AddAfter(node, i);
            }

            stopWatch.Stop();
            TimeSpan ts2 = stopWatch.Elapsed;

            Console.WriteLine("Time to insert {0} objects in the middle of LinkedList: {1}", size, ts2);

            if (ts > ts2)
            {
                Console.WriteLine("\tLinkedList is quicker!");
                return winner = "LinkedList";
            }
            else
            {
                Console.WriteLine("\tArrayList is quicker!");
                return winner = "ArrayList";
            }
        }
Exemple #3
0
        private string insertElementsInEnd(ArrayList al, LinkedList<int> ll)
        {
            string winner;
            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            int size = 1000;

            for (int i = 0; i < size; i++)
            {
                ExampleClass ec = new ExampleClass();
                al.Add(ec);
            }

            int size2 = 100;
            for (int i = 0; i < size2; i++)
            {
                al.Insert(size, i);
            }

            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;

            Console.WriteLine("\n\n=================================================================================");
            Console.WriteLine("Time to insert {0} elements in the end of ArrayList: {1}", size, ts);

            stopWatch.Reset();
            stopWatch.Start();

            for (int i = 0; i < size; i++)
            {
                ll.AddFirst(i);
            }

            for (int i = 0; i < size2; i++)
            {
                ll.AddLast(i);
            }

            stopWatch.Stop();
            TimeSpan ts2 = stopWatch.Elapsed;

            Console.WriteLine("Time to insert {0} objects in the end of LinkedList: {1}", size, ts2);

            if (ts > ts2)
            {
                Console.WriteLine("\tLinkedList is quicker!");
                return winner = "LinkedList";
            }
            else
            {
                Console.WriteLine("\tArrayList is quicker!");
                return winner = "ArrayList";
            }
        }