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;
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:
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:
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:
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
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();
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);
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);
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);
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);
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 = "";
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
}
}