2013-04-11 14 views
1

我有一个家庭作业,我必须建设性地破坏性地颠倒一个数组列表,并为不同长度的列表计时。我的Arraylist每次运行时都更新,但似乎没有在这些方法中注册,因为我没有获得我的计时值,也无法找到我的错误。计时建设性的和破坏性的逆转

我的代码到目前为止如下。

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("---------------------------------------------------------"); 
    } 

你们能否请我指出正确的方向,为什么我没有获得计时值?我不想确切的答案,而只是帮助理解。

谢谢

+0

你的程序的输出是什么?你是什​​么意思,你没有得到价值? TimeSpan不是空的,所以你必须得到一些东西。 – Blorgbeard 2013-04-11 04:34:36

+2

考虑使用秒表而不是日期时间(因为你的代码可能需要少于15ms) – 2013-04-11 04:36:47

+2

你应该[使用秒表](http://stackoverflow.com/questions/2923283/stopwatch-vs-using-system- datetime-now-for-timing-events)来测量这个 – V4Vendetta 2013-04-11 04:37:09

回答

1

您宁愿有一个计时器类。您的计时方法未考虑垃圾收集和终结器方法。

下面是一个例子,然后

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; 
    } 


} 

您的代码会是这样的

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("---------------------------------------------------------"); 

,不应该在执行你的逆转方法之前,克隆的名单?如果您打算克隆它们,请在启动计时器和反转方法之前克隆它们。

1

你不想从DateTime.Substract DateTime。只需改用TimeSpan(DateTime.Now-startTime)并打印即可。您可能想要打印Total Miliseconds,因为这种操作很快,因为这种操作很快