<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Michael E. Chancey Jr. &#187; Efficiency</title>
	<atom:link href="http://michael.chanceyjr.com/tag/efficiency/feed/" rel="self" type="application/rss+xml" />
	<link>http://michael.chanceyjr.com</link>
	<description></description>
	<lastBuildDate>Fri, 20 May 2011 03:43:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Execution Timer For Helping With Efficiency</title>
		<link>http://michael.chanceyjr.com/useful-code/execution-timer-for-helping-with-efficiency/</link>
		<comments>http://michael.chanceyjr.com/useful-code/execution-timer-for-helping-with-efficiency/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 05:00:26 +0000</pubDate>
		<dc:creator>Michael E. Chancey Jr.</dc:creator>
				<category><![CDATA[Useful Code]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C-Sharp]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Efficiency]]></category>
		<category><![CDATA[Timing]]></category>

		<guid isPermaLink="false">http://michael.chanceyjr.com/?p=481</guid>
		<description><![CDATA[This class is useful when checking times which functions take to execute. Simply instantiate this class and when you are done timing call the StopTimer function and then you can display how long something too to execute. I use this class all over the place when I have three different ideas but am not sure [...]]]></description>
			<content:encoded><![CDATA[<p>This class is useful when checking times which functions take to execute.  Simply instantiate this class and when you are done timing call the StopTimer function and then you can display how long something too to execute.  I use this class all over the place when I have three different ideas but am not sure which one will execute faster.</p>
<h3>//Class</h3>
<hr/>
<pre class="brush: csharp;">
    class ExecutionTimer
    {
        //PRIVATE VARIABLES FOR HOLDING THE TIME INFORMATION
        private long startTime;
        private long stopTime;

        //DEFAULT CONSTRUCTOR JUST CALLS THE START TIMER
        public ExecutionTimer()
        {
            StartTimer();
        }

        /// &lt;summary&gt;
        /// MARK THE START TIME FOR CALCULATIONS
        /// &lt;/summary&gt;
        public void StartTimer()
        {
            startTime = DateTime.Now.Ticks;
        }

        /// &lt;summary&gt;
        /// MARK THE STOP TIME FOR CALCULATIONS
        /// &lt;/summary&gt;
        public void StopTimer()
        {
            stopTime = DateTime.Now.Ticks;
        }

        /// &lt;summary&gt;
        /// DEFAULT OVERRIDE OF TOSTRING TO FORMAT THE OUTPUT AS A VALID TIME STRING
        /// &lt;/summary&gt;
        /// &lt;returns&gt;FORMATTED TIME STRING IN FORMAT OF HH:MM:SS.MILISECONDS&lt;/returns&gt;
        public override string ToString()
        {
            return ToString(1);
        }

        /// &lt;summary&gt;
        /// OUTPUT THE STRING USING AN AVERAGE COUNTER INCASE THE USER RAN THE TRACKING MORE THEN ONCE WE NEED TO AVG THE TIME
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;avg&quot;&gt;DIVISOR&lt;/param&gt;
        /// &lt;returns&gt;FORMATTED TIME STRING IN FORMAT OF HH:MM:SS.MILISECONDS AVERAGED OVER X ATTEMPTS&lt;/returns&gt;
        public string ToString(long avg)
        {
            //VERIFY THAT AVG IS A VALID VALUE
            if (avg == 0)
                throw new ArgumentException(&quot;Avg can not be 0&quot;, &quot;avg&quot;);

            //RETURN THE FORMATTED STRING FROM THE NEW DATE
            return string.Format(&quot;{0:HH:mm:ss.ff}&quot;, new DateTime((stopTime - startTime) / avg));
        }
    }
</pre>
<h3>//Usage</h3>
<hr/>
<pre class="brush: csharp;">
    class Program
    {
        static void Main(string[] args)
        {
            ExecutionTimer tmpTimer = new ExecutionTimer();
            System.Threading.Thread.Sleep(5500);
            tmpTimer.StopTimer();
        }
    }
</pre>



Share


	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Execution%20Timer%20For%20Helping%20With%20Efficiency%20-%20http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fexecution-timer-for-helping-with-efficiency%2F" title="Twitter"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fexecution-timer-for-helping-with-efficiency%2F&amp;title=Execution%20Timer%20For%20Helping%20With%20Efficiency&amp;bodytext=This%20class%20is%20useful%20when%20checking%20times%20which%20functions%20take%20to%20execute.%20%20Simply%20instantiate%20this%20class%20and%20when%20you%20are%20done%20timing%20call%20the%20StopTimer%20function%20and%20then%20you%20can%20display%20how%20long%20something%20too%20to%20execute.%20%20I%20use%20this%20class%20all%20over%20t" title="Digg"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fexecution-timer-for-helping-with-efficiency%2F&amp;title=Execution%20Timer%20For%20Helping%20With%20Efficiency" title="StumbleUpon"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fmichael.chanceyjr.com%2Fuseful-code%2Fexecution-timer-for-helping-with-efficiency%2F&amp;title=Execution%20Timer%20For%20Helping%20With%20Efficiency&amp;notes=This%20class%20is%20useful%20when%20checking%20times%20which%20functions%20take%20to%20execute.%20%20Simply%20instantiate%20this%20class%20and%20when%20you%20are%20done%20timing%20call%20the%20StopTimer%20function%20and%20then%20you%20can%20display%20how%20long%20something%20too%20to%20execute.%20%20I%20use%20this%20class%20all%20over%20t" title="del.icio.us"><img src="http://michael.chanceyjr.com/site/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://michael.chanceyjr.com/useful-code/execution-timer-for-helping-with-efficiency/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

