CStopWatch - 秒表风格的代码执行速度测试






2.67/5 (23投票s)
2003年12月9日

55020

486
一个非常简单易用的秒表类,用于代码执行速度测试。
下载源代码文件 - 2.51 Kb
引言
一个非常简单易用的秒表类,用于代码执行速度测试。只需将 enumCStopWatchStateMachine
和 CStopWatch
的代码粘贴到您的项目中,并调用 reset()
将秒表重置为 0。调用 start()
启动秒表。调用 stop()
停止秒表。调用 getTimeEllapsedInMilliseconds()
获取秒表中的当前毫秒数。
由 Chatbot! Cyber Community - 人类与聊天机器人联合的赛博社区! 呈现。
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
}
这个类的工作方式就像一个手持秒表。
public class CStopWatch
{
System.DateTime m_tmStartDateTime;
enumCStopWatchStateMachine m_enumState;
int m_intTimeEllapsedInMilliseconds;
这些是秒表支持的事件
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;
}
}
}
这些是秒表可以处于的状态以及每个状态的操作。
在此状态下,将缓存的差值清零。
protected void stateSTOPPED_AND_NEVER_BEEN_STARTED()
{
m_intTimeEllapsedInMilliseconds = 0;
}
在此状态下,返回 0。
protected int stateRETURNING_ZERO()
{
return 0;
}
在此状态下,将起始 DateTime
重置为 Now
。
protected void stateBEENSTARTED_AND_COUNTING()
{
m_tmStartDateTime = System.DateTime.Now;
}
在此状态下,返回起始 datetime
和 now
之间的时间差 timeEllapsed
+ 缓存的差值。
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;
}
在此状态下,将缓存的差值设置为起始 datetime
和 now
之间的时间差 timeEllapsed
+ 缓存的差值。
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;
}
在此状态下,返回缓存的差值。
protected int stateRETURNING_CASHED_TIME_DIFFERENCE()
{
return m_intTimeEllapsedInMilliseconds;
}
}
将 Windows 事件绑定到 CStopWatch
事件。
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
}
}