1

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

user2046257
  • 157
  • 2
  • 10
  • What is the output of your program? What do you mean you aren't getting values? TimeSpan is not nullable, so you must be getting something. – Blorgbeard Apr 11 '13 at 04:34
  • 2
    Consider using Stopwatch instead of DateTime (as your code is way likely to take less than 15ms) – Alexei Levenkov Apr 11 '13 at 04:36
  • 2
    you should be [using a Stopwatch](http://stackoverflow.com/questions/2923283/stopwatch-vs-using-system-datetime-now-for-timing-events) to measure this – V4Vendetta Apr 11 '13 at 04:37
  • 1
    It prints the list of developed and time taken and then does this again for 1,5,10,20,..,15000 items in developed but My time taken usually is 00:00:00...except for the really high values. Then only my constructive is 00:00:00 – user2046257 Apr 11 '13 at 04:37
  • 1
    Well it's supposed to be that way. You need to run millions of this simple instructions to measure anything. Remember simply opening this page your computer already run hundreds of complex instructions – Martheen Apr 11 '13 at 04:41
  • Ok thanks. I'll try using stopwatch to measure it. – user2046257 Apr 11 '13 at 04:43

2 Answers2

1

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

Martheen
  • 5,198
  • 4
  • 32
  • 55
1

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.