Вопрос

I have a homework assignment where I must constructively and destructively reverse an array list and time it for varying lengths of the list. My Arraylist does update each time I run it but it doesn't seem to register in these methods as I'm not getting values for my timing and can't seem to find my errors.

My code so far is as follows.

public ArrayList ConstructiveReverseDeveloped()
    {            
        ArrayList Temp = new ArrayList();
        for (int i = Developed.Count - 1; i >= 0; i--)
        {
            Apps cur = (Apps)Developed[i];
            Temp.Add(cur);
        }
        return Temp;
    }
    public void TimingConstructive()
    {
        DateTime startTime;
        TimeSpan endTime;
        startTime = DateTime.Now;
        ConstructiveReverseDeveloped();
        endTime = DateTime.Now.Subtract(startTime);
        Console.WriteLine("---------------------------------------------------------");
        Console.WriteLine("Time taken for Constructive Reverse of Developed : {0}", endTime);
    }

public void DestructiveReverseDeveloped()
    {
        //ArrayList x = cloneDeveloped();
        for (int i = Developed.Count - 1; i >= 0; i--)
        {
            Apps cur = (Apps)Developed[i];
            Developed.RemoveAt(i);
            Developed.Add(cur);
        }
    }
    public void TimingDestructive()
    {
        DateTime startTime;
        TimeSpan endTime;
        startTime = DateTime.Now;
        DestructiveReverseDeveloped();
        endTime = DateTime.Now.Subtract(startTime);
        Console.WriteLine("Time taken for Destructive Reverse of Developed : {0}",endTime.ToString());
        Console.WriteLine("---------------------------------------------------------");
    }

Can you guys please point me in the right direction as to why I'm not getting timing values? I don't want the exact answers but rather just help in understanding.

Thanks

Это было полезно?

Решение

You'd rather have a timer class. Your timing method isn't taking into consideration garbage collection and finalizer methods.

Here is an Example

class Timer
{
    private DateTime startingTime;
    // stores starting time of code being tested
    private TimeSpan duration;
    // stores duration of code being tested
    public void startTime()
    {
        GC.Collect();   // force garbage collection
        GC.WaitForPendingFinalizers();
        /* wait until all heap contents finalizer methods have completed for removal of contents to be permanent */
        startingTime = DateTime.Now;
        // get current date/time
    }
    public void stopTime()
    {
        // .Subtract: TimeSpan subtraction
        duration = DateTime.Now.Subtract(startingTime);
    }

    public TimeSpan result()
    {
        return duration;
    }


}

Your code would then be something like

public void TimingDestructive()
{
    Timer Time = new Timer();
    Time.startTime();
    DestructiveReverseDeveloped();
    Time.stopTime();
    Console.WriteLine("Time taken for Destructive Reverse of Developed : {0}ms",Time.result().TotalMilliseconds);
    Console.WriteLine("---------------------------------------------------------");

And shouldn't you clone your Lists before performing your reversal methods? If you do plan to clone them, clone them right before starting your timer and reversal method.

Другие советы

You don't want the DateTime from DateTime.Substract. Simply get TimeSpan instead (DateTime.Now-startTime) and print it. You might wanna print the Total Miliseconds instead since this kind of operation is very quick

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top