Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C# Write a console application to determine the time required to perform t

ID: 3809398 • Letter: U

Question

Using C# Write a console application to determine the time required to perform the push/pop or queue/dequeue operations on your stacks and queues. Make an instance of a C# stack and a C# queue. Get the time required to push 10,000,000 values onto each stack and then get the time required to pop 10,000,000 values from each stack. Repeat the process using the queue and dequeue operations with the queues. Your console app should be similar to the one we used for lab 7 with the linked lists and lists. Please post code with comments explaining.

Explanation / Answer

using System;
using System.Collections;
using System.Diagnostics;

namespace Chegg_console
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack st = new Stack(); //new stack object
            Queue q = new Queue(); //new queue object
            Stopwatch sw = new Stopwatch(); // used to measure time elapsed
            sw.Start(); //start stop watch
            for (long i = 0; i < 10000000; i++)
                st.Push("A"); //push
            sw.Stop(); // stop the stop watch
            Console.WriteLine("10,000,000 stack push required "+ sw.Elapsed); // plint the elapsed time

            sw.Start(); // start the stopwarch again
            for (long i = 0; i < 10000000; i++)
                st.Pop(); //pop all elements
            sw.Stop();
            Console.WriteLine("10,000,000 stack pop required "+ sw.Elapsed); //print elapsed time

            sw.Start(); //restart timer
            for (long i = 0; i < 10000000; i++)
                q.Enqueue("A"); //enqueue
            sw.Stop(); //stop timer
            Console.WriteLine("10,000,000 enqueue required " + sw.Elapsed); //print

            sw.Start(); //restart time
            for (long i = 0; i < 10000000; i++)
                q.Dequeue(); //dequeue
            sw.Stop(); //stop stopwatch
            Console.WriteLine("10,000,000 dequeue required " + sw.Elapsed);

            Console.Read(); //to hold the console display
        }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote