2012-12-03 58 views
7

在這個例子中的代碼我使用的是簡單的秒錶來測試時,它需要完成特定任務的性能測試/動作Helper類使用秒錶類

StopWatch SW1 = new StopWatch(); 
SW1.Start(); 
SomeAction(); 
SW1.Stop(); 
sting results = SW1.Elapsed.ToString(); 
MessageBox.Show(resutls); 

我想有一類,我將實例與測試

public Class PerformanceTests 
{ 
    public StopWatch SW1 = new StopWatch(); 
    public StopWatch SW2 = new StopWatch(); 
    public string results1 = "", results2 = ""; 
    .... 
    .... 
    //some other variables to use 
} 

雖然實例化類,並嘗試使用SW1是不是讓我使用它的方法時使用。我究竟做錯了什麼 ?

PerformanceTests Ptst = new PerformanceTests(); 
Ptst.SW1. ... Start() is not accessible 

更新

答案的休息,不要將代碼從我複製,因爲我錯過資本stopwatch。而不是實例化Stopwatch類,我不小心沒有注意到Visual Studio詢問我是否想爲我所謂的秒錶創建一個類,而不是.NET的真實Stopwatch

因此,我的建議是,儘管應始終保持相同,但請注意Visual Studio intellisense 的建議操作。只要確保直到你真的有經驗並且知道所有的課程。

+0

'intelliSense'聽起來像蘋果產品。編輯,和許多 – nawfal

+0

我在這裏寫了一個答案:http:// stackoverflow。com/questions/969290 /精確時間測量性能測試/ 16157458#16157458 另請參閱http://stackoverflow.com/questions/232848/wrapping-stopwatch-timing-with-a-delegate-or -lambda – nawfal

回答

19

下面是簡單的類,它可以幫助你測量代碼塊的執行時間:

public class PerformanceTester : IDisposable 
{ 
    private Stopwatch _stopwatch = new Stopwatch(); 
    private Action<TimeSpan> _callback; 

    public PerformanceTester() 
    { 
     _stopwatch.Start(); 
    } 

    public PerformanceTester(Action<TimeSpan> callback) : this() 
    { 
     _callback = callback;    
    } 

    public static PerformanceTester Start(Action<TimeSpan> callback) 
    { 
     return new PerformanceTester(callback); 
    } 

    public void Dispose() 
    { 
     _stopwatch.Stop(); 
     if (_callback != null) 
      _callback(Result); 
    } 

    public TimeSpan Result 
    { 
     get { return _stopwatch.Elapsed; } 
    } 
} 

使用(只是換行代碼塊使用的PerformanceTester):

using (var tester = new PerformanceTester()) 
{ 
    // code to test 
    MessageBox.Show(tester.Results.ToString()); 
} 

如果前聲明變量測試儀塊,然後秒錶會自動在您退出using塊停下來,結果將是爲您提供:

PerformanceTester tester; 

using (tester = new PerformanceTester())  
    SomeAction(); 

MessageBox.Show(tester.Results.ToString()); 

如果傳遞的回調動作PerformanceTester,那麼這一行動將在using語句到底叫什麼,而經過時間將被傳遞到回調:

using (PerformanceTester.Start(ts => MessageBox.Show(ts.ToString()))) 
    SomeAction(); 

你可以聲明方法,將接受TimeSpan和處理結果:

private void ProcessResult(TimeSpan span) 
{ 
    // log, show, etc 
    MessageBox.Show(span.ToString()); 
} 

使用變得非常乾淨:

using (PerformanceTester.Start(ProcessResult)) 
    SomeAction(); 
+0

這是strangley,而我正在採取intelliSence建議,而不是使用statment我創建一個班,因爲我missSpelled(大寫)*我認爲它應該是! 'StopWatch()'! (: 謝謝。沒有這個錯誤的原因,我不會有機會看到你整潔的ext'方法!+1對於酷類 – LoneXcoder

+0

順便說一句,它也讓你避免使用不必要的進口其他類的點.net在應用程序webform/winform中有沒有幫助,我的意思是說,我儘量少使用''使用'語句,意味着更少的類導入,它有什麼作用,或者當我instanciate然後,我使用.net類(System.diagnostics),所以它至少在應用程序的結果大小方面並不重要。 – LoneXcoder

+0

如何聲明使用測試程序(之前)可以給你另一個例子,如在你的評論下面你的答案「如果你聲明測試變量之前使用'塊'...」你可以擴大一點 – LoneXcoder

1

讓他們公開,而不是私有:

public class PerformanceTests 
    { 
     public StopWatch SW1 { get; set; } 

     public StopWatch SW2 { get; set; } 

     public string Results1 { get; set; } 

     public string Results2 { get; set; } 

     public PerformanceTests() 
     { 
      this.SW1 = new StopWatch(); 
      this.SW2 = new StopWatch(); 
     } 
    } 
0

除非您使用的是自定義類秒錶是不正確的類名試圖Stopwatch的命名空間下System.Diagnostics

嘗試:

public Class PerformanceTests 
{ 
    public Stopwatch SW1 = new Stopwatch(); 
    public Stopwatch SW2 = new Stopwatch(); 
    public string results1 = "", results2 = ""; 
    .... 
    .... 
    //some other variables to use 
}