2013-04-11 47 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,因爲這種操作很快,因爲這種操作很快