Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CStopWatch - Stopwatch style code execution speed tests

0.00/5 (No votes)
8 Dec 2003 1  
A very simple and easy to use stopwatch class for code execution speed tests.
Download source files - 2.51 Kb

Introduction

A very simple and easy to use stopwatch class for code execution speed tests. Simply paste the code for enumCStopWatchStateMachine and CStopWatch into your project and call reset() to reset the stopwatch to 0. Call start() to start the stopwatch. Call stop() to stop the stop watch. Call getTimeEllapsedInMilliseconds() to get the current milliseconds in the stop watch.

Brought to you by the Chatbot! Cyber Community - Cyber community where humans and chatbots unite!

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace InCodeTimeTest{

    public enum enumCStopWatchStateMachine
    {
      STOPPED_AND_NEVER_BEEN_STARTED,
      RETURNING_ZERO,
      BEENSTARTED_AND_COUNTING,
      RETURNING_DIFFERENCE_PLUS_CASHED_TIME_DIFFERENCE,
      BEEN_STARTED_AND_NOT_COUNTING,
      RETURNING_CASHED_TIME_DIFFERENCE
    }

This class works just like a hand held stop watch.

    public class CStopWatch
    {
      System.DateTime m_tmStartDateTime;
      enumCStopWatchStateMachine m_enumState;
      int m_intTimeEllapsedInMilliseconds;

Sample screenshot

These are the events that the stop watch supports:

      void CSpeedTest()
      {
       lock(this)
       {
        stateSTOPPED_AND_NEVER_BEEN_STARTED();
        m_enumState = enumCStopWatchStateMachine.STOPPED_AND_NEVER_BEEN_STARTED;
       }
      }
      public int getTimeEllapsedInMilliseconds()
      {
       lock(this)
       {
        switch(m_enumState)
        {
         case enumCStopWatchStateMachine.STOPPED_AND_NEVER_BEEN_STARTED:
          return stateRETURNING_ZERO();
         case enumCStopWatchStateMachine.BEENSTARTED_AND_COUNTING:
          return stateRETURNING_DIFFERENCE_PLUS_CASHED_TIME_DIFFERENCE();
         case enumCStopWatchStateMachine.BEEN_STARTED_AND_NOT_COUNTING:
          return stateRETURNING_CASHED_TIME_DIFFERENCE();
        }
        return 0;
       }
      }
      public void reset()
      {
       lock(this)
       {
        m_enumState = enumCStopWatchStateMachine.STOPPED_AND_NEVER_BEEN_STARTED;
        stateSTOPPED_AND_NEVER_BEEN_STARTED();
       }
      }
      public void start()
      {
       lock(this)
       {
        switch(m_enumState)
        {
         case enumCStopWatchStateMachine.STOPPED_AND_NEVER_BEEN_STARTED:
          m_enumState = enumCStopWatchStateMachine.BEENSTARTED_AND_COUNTING;
          stateBEENSTARTED_AND_COUNTING();
          break;
         case enumCStopWatchStateMachine.BEENSTARTED_AND_COUNTING:
          //do nothing

          break;
         case enumCStopWatchStateMachine.BEEN_STARTED_AND_NOT_COUNTING:
          m_enumState = enumCStopWatchStateMachine.BEENSTARTED_AND_COUNTING;
          stateBEENSTARTED_AND_COUNTING();
          break;
        }
       }
      }
      public void stop()
      {
       lock(this)
       {
        switch(m_enumState)
        {
         case enumCStopWatchStateMachine.STOPPED_AND_NEVER_BEEN_STARTED:
          //do nothing

          break;
         case enumCStopWatchStateMachine.BEENSTARTED_AND_COUNTING:
          m_enumState = enumCStopWatchStateMachine.BEEN_STARTED_AND_NOT_COUNTING;
          stateBEEN_STARTED_AND_NOT_COUNTING();
          break;
         case enumCStopWatchStateMachine.BEEN_STARTED_AND_NOT_COUNTING:
          //do nothing

          break;
        }
       }
      }

These are the states that the stop watch can be in and the action of each state.

In this state, zero out cashed difference.

      protected void stateSTOPPED_AND_NEVER_BEEN_STARTED()
      {
       m_intTimeEllapsedInMilliseconds = 0;
      }

In this state, return 0.

      protected int stateRETURNING_ZERO()
      {
       return 0;
      }

In this state, reset start DateTime to Now.

      protected void stateBEENSTARTED_AND_COUNTING()
      {
       m_tmStartDateTime = System.DateTime.Now;
      }

In this state, return timeEllapsed between start datetime and now + cached difference.

      protected int stateRETURNING_DIFFERENCE_PLUS_CASHED_TIME_DIFFERENCE()
      {
       System.DateTime tmNow = System.DateTime.Now;
       return ((((((tmNow.Hour * (24 * 60 * 1000)) + 
        (tmNow.Minute * (60 * 1000))) + 
        (tmNow.Second * 1000)) + tmNow.Millisecond) - 
        ((((m_tmStartDateTime.Hour * 
        (24 * 60 * 1000)) + (m_tmStartDateTime.Minute * 
        (60 * 1000))) + (m_tmStartDateTime.Second * 1000)) + 
        m_tmStartDateTime.Millisecond))) + 
        m_intTimeEllapsedInMilliseconds;
      }

In this state, set cached difference to timeEllapsed between start datetime and now + cached difference.

      protected void stateBEEN_STARTED_AND_NOT_COUNTING()
      {
       System.DateTime tmNow = System.DateTime.Now;
       m_intTimeEllapsedInMilliseconds = ((((((tmNow.Hour * 
        (24 * 60 * 1000)) + 
        (tmNow.Minute * (60 * 1000))) + 
        (tmNow.Second * 1000)) + tmNow.Millisecond) - 
        ((((m_tmStartDateTime.Hour * (24 * 60 * 1000)) + 
        (m_tmStartDateTime.Minute * (60 * 1000))) + 
        (m_tmStartDateTime.Second * 1000)) + 
        m_tmStartDateTime.Millisecond))) + 
        m_intTimeEllapsedInMilliseconds;
      }

In this state, return cashed difference.

      protected int stateRETURNING_CASHED_TIME_DIFFERENCE()
      {
       return m_intTimeEllapsedInMilliseconds;
      }
     }

Bind Windows events to CStopWatch events.

     public class CInCodeTimeTest : System.Windows.Forms.Form
     {
      CStopWatch m_StopWatch;
      private void CInCodeTimeTest_Load(object sender, System.EventArgs e)
      {m_StopWatch = new CStopWatch();}
      private void cmdReset_Click(object sender, System.EventArgs e)
      {m_StopWatch.reset();}
      private void cmdStart_Click(object sender, System.EventArgs e)
      {m_StopWatch.start();}
      private void cmdStop_Click(object sender, System.EventArgs e)
      {m_StopWatch.stop();}
      private void cmdDisplay_Click(object sender, System.EventArgs e)
      {txtOutPut.Text = m_StopWatch.getTimeEllapsedInMilliseconds().ToString();
      
      
      
      }
      private System.Windows.Forms.Button cmdReset;
      private System.Windows.Forms.Button cmdStart;
      private System.Windows.Forms.Button cmdStop;
      private System.Windows.Forms.Button cmdDisplay;
      private System.Windows.Forms.TextBox txtOutPut;
      #region ConstructAndDispose
      private System.ComponentModel.Container components = null;
      public CInCodeTimeTest(){
       InitializeComponent();
      }
      protected override void Dispose( bool disposing )
      {
       if( disposing ){
        if (components != null) {
         components.Dispose();
        }
       }
       base.Dispose( disposing );
      }
      #endregion
      #region Windows Form Designer generated code
      // <summary>

      // Required method for Designer support - do not modify

      // the contents of this method with the code editor.

      // </summary>

      private void InitializeComponent()
      {
       this.cmdReset = new System.Windows.Forms.Button();
       this.cmdStart = new System.Windows.Forms.Button();
       this.cmdStop = new System.Windows.Forms.Button();
       this.cmdDisplay = new System.Windows.Forms.Button();
       this.txtOutPut = new System.Windows.Forms.TextBox();
       this.SuspendLayout();
       // 

       // cmdReset

       // 

       this.cmdReset.Location = new System.Drawing.Point(0, 8);
       this.cmdReset.Name = "cmdReset";
       this.cmdReset.Size = new System.Drawing.Size(72, 24);
       this.cmdReset.TabIndex = 1;
       this.cmdReset.Text = "Reset";
       this.cmdReset.Click += new System.EventHandler(this.cmdReset_Click);
       // 

       // cmdStart

       // 

       this.cmdStart.Location = new System.Drawing.Point(72, 8);
       this.cmdStart.Name = "cmdStart";
       this.cmdStart.Size = new System.Drawing.Size(72, 24);
       this.cmdStart.TabIndex = 2;
       this.cmdStart.Text = "Start";
       this.cmdStart.Click += new System.EventHandler(this.cmdStart_Click);
       // 

       // cmdStop

       // 

       this.cmdStop.Location = new System.Drawing.Point(144, 8);
       this.cmdStop.Name = "cmdStop";
       this.cmdStop.Size = new System.Drawing.Size(72, 24);
       this.cmdStop.TabIndex = 3;
       this.cmdStop.Text = "Stop";
       this.cmdStop.Click += new System.EventHandler(this.cmdStop_Click);
       // 

       // cmdDisplay

       // 

       this.cmdDisplay.Location = new System.Drawing.Point(216, 8);
       this.cmdDisplay.Name = "cmdDisplay";
       this.cmdDisplay.Size = new System.Drawing.Size(72, 24);
       this.cmdDisplay.TabIndex = 4;
       this.cmdDisplay.Text = "Display";
       this.cmdDisplay.Click += new System.EventHandler(this.cmdDisplay_Click);
       // 

       // txtOutPut

       // 

       this.txtOutPut.Location = new System.Drawing.Point(92, 126);
       this.txtOutPut.Name = "txtOutPut";
       this.txtOutPut.Size = new System.Drawing.Size(104, 20);
       this.txtOutPut.TabIndex = 9;
       this.txtOutPut.Text = "";
       // 

       // CInCodeTimeTest

       // 

       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
       this.ClientSize = new System.Drawing.Size(288, 273);
       this.Controls.AddRange(new System.Windows.Forms.Control[] {
                       this.txtOutPut,
                       this.cmdDisplay,
                       this.cmdStop,
                       this.cmdStart,
                       this.cmdReset});
       this.Name = "CInCodeTimeTest";
       this.Text = "In Code Time Test";
       this.Load += new System.EventHandler(this.CInCodeTimeTest_Load);
       this.ResumeLayout(false);
      }
      #endregion
      #region Main
      [STAThread]
      static void Main() 
      {
       Application.Run(new CInCodeTimeTest());
      }
      #endregion Main
     }
    }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here