Important alert: (current site time 7/15/2013 7:46:06 AM EDT)
 

VB icon

[ A simple] code execution speed test class

Email
Submitted on: 10/9/2003 1:15:50 AM
By: lonetron 
Level: Advanced
User Rating: By 2 Users
Compatibility: C#
Views: 20613
author picture
(About the author)
 
     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

 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: [ A simple] code execution speed test class
// Description: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
// By: lonetron
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1627&lngWId=10//for details.//**************************************

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace InCodeTimeTest{
	/*
				-----------------------------------------------------------------------------
				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
				-----------------------------------------------------------------------------
	*/
	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
	}
	//Works just like a stop watch
	public class CStopWatch
	{
		System.DateTime m_tmStartDateTime;
		enumCStopWatchStateMachine m_enumState;
		int m_intTimeEllapsedInMilliseconds;
		//events
		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;
				}
			}
		}
		//States
		protected void stateSTOPPED_AND_NEVER_BEEN_STARTED()
		{
			//zero out cashed difference
			m_intTimeEllapsedInMilliseconds = 0;
		}
		protected int stateRETURNING_ZERO()
		{
			//return 0
			return 0;
		}
		protected void stateBEENSTARTED_AND_COUNTING()
		{
			//reset start datetime to now
			m_tmStartDateTime = System.DateTime.Now;
		}
		protected int stateRETURNING_DIFFERENCE_PLUS_CASHED_TIME_DIFFERENCE()
		{
			//return timeEllapsed between start datetime and now + cashed 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;
		}
		protected void stateBEEN_STARTED_AND_NOT_COUNTING()
		{
			//cashed difference = timeEllapsed between start datetime and now + cashed difference
			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 cashed difference
			return m_intTimeEllapsedInMilliseconds;
		}
	}
	public class CInCodeTimeTest : System.Windows.Forms.Form
	{
		/*
					 _________________________________________________________________________
					|This simple solution was brought to you by the P2B Consortium.|
					|“Create your very own artificial intelligence chat-bot now!”|
					|http://www.p2bconsortium.com/|
					||
					|Create, deploy and test artificial intelligence chat-bot |
					|now with Rapid Bot Trainer: http://www.p2bconsortium.com/sss/rbt.aspx |
					||
					|Create, deploy and test artificial intelligence chat-bot |
					|now with ANY IRC CLIENT and no downloads:|
					|http://www.p2bconsortium.com/stork.html |
					||
					|Create, test and deploy an artificial intelligence chat-bot|
					|to ANY ONLINE CHATROOM with the TANU public API web methods:|
					|http://www.p2bconsortium.com/Simple%20State%20Server%20Tutorial.htm |
					|http://www.p2bconsortium.com/sss/sss.asmx|
					|_________________________________________________________________________|
		*/
		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
	}
}


Other 9 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
3/30/2005 9:02:14 PM

please take code always in zip file to easily download
(If this comment was disrespectful, please report it.)

 
4/13/2006 3:59:44 AMk seshagiri

Very useful for learners
(If this comment was disrespectful, please report it.)

 
2/16/2010 5:40:02 AMport dinleme

portdinleme class dosyası
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.