Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8934565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:49:11+00:00 2026-06-15T09:49:11+00:00

In this example code i am using a simple StopWatch to test time that

  • 0

In this example code i am using a simple StopWatch to test time that 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 instantiate to 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 
}

though when instantiating the class and trying to use SW1 is not letting me use its methods. What am i doing wrong ?

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

Update

For rest of answers, don’t copy the code from me, as I miss capitalized stopwatch. Instead of instantiating the Stopwatch class i accidentally didn’t pay attention Visual Studio asking if i want to create a class for my so called stopwatch instead of .NET’s real Stopwatch.

So my advice, pay attention to the suggested actions of Visual Studio intellisense
even though it should be same all the time . Just make sure till you’re really experienced and know all the classes by heart.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:49:12+00:00Added an answer on June 15, 2026 at 9:49 am

    Here is simple class, which can help you for measuring time of code block execution:

    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 (just wrap code block with using of PerformanceTester):

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

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

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

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

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

    You can declare method, which will accept TimeSpan and process results:

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

    Usage becomes very clean:

    using (PerformanceTester.Start(ProcessResult))
         SomeAction();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using this simple node.js code example but I don't understand why FireBug doesn't
I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that
This is the example code, I'm using these functions for a program, but scanf
I'm using this plugin: http://www.jeremymartin.name/projects.php?project=kwicks And my code follows this example: http://www.jeremymartin.name/examples/kwicks.php?example=7 I'm using
If I have a simple cucumber feature and scenario , like this (example code
With regards this example from Code Complete: Comparison Compare(int value1, int value2) { if
I suppose that this is an interesting code example. We have a class --
I am using a code example block in an XML comment as a simple
Consider this simple example code: <form name=text id=frm1 method=post> <input type=checkbox name=name[] value=1000> Chk1<br>
i am following on this example Simple example of using data from a YAML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.