Helper class for performance tests using the StopWatch class

In this code example, I use a simple StopWatch to check the time it takes to complete a given task / action

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

I would like to have a class that I will create for use with tests

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

although you are not allowed to use its methods when instantiating a class and using SW1 . What am I doing wrong?

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

Update

For the rest of the answers, do not copy the code with me, as I am missing the stopwatch capital letters. Instead of instantiating a stopwatch class, I accidentally ignored Visual Studio asking if I wanted to create a class for my so-called stopwatch instead of the .NET real stopwatch .

So, my advice is, pay attention to the suggested Visual Studio intellisense actions, although it should be the same all the time. Just make sure that you are truly experienced and know all the names by heart.

+7
source share
3 answers

Here is a simple class that will help you measure the code execution time:

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

Usage (only code block code using PerformanceTester ):

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

If you specify the tester variable before the using block, then the stopwatch will automatically stop when you exit the using block, and the results will be available to you:

 PerformanceTester tester; using (tester = new PerformanceTester()) SomeAction(); MessageBox.Show(tester.Results.ToString()); 

If you pass the callback action to PerformanceTester , this action will be called at the end of the using statement, and the elapsed time will be passed to the callback:

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

You can declare a method that takes a TimeSpan and processes the results:

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

Usage becomes very clean:

 using (PerformanceTester.Start(ProcessResult)) SomeAction(); 
+19
source

make them public, not private:

  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(); } } 
+1
source

If you are not using a custom class, then StopWatch is not a valid class name. Stopwatch in the System.Diagnostics namespace

Try:

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

All Articles