<?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>Martijn's C# Programming Blog &#187; stopwatch</title>
	<atom:link href="http://www.dijksterhuis.org/tag/stopwatch/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Timing C# function performance with the Stopwatch class</title>
		<link>http://www.dijksterhuis.org/timing-function-performance-stopwatch-class/</link>
		<comments>http://www.dijksterhuis.org/timing-function-performance-stopwatch-class/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 03:55:18 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[function execution]]></category>
		<category><![CDATA[stopwatch]]></category>
		<category><![CDATA[timing]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=458</guid>
		<description><![CDATA[
Occasionally you need to time the performance of a C# function to see if its as fast, or as slow, as you think it is. You could of course run your code through a profiler. But in situations that that would be overkill there exists a quick solution. Originally I looked at the standard DateTime [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><em><img class="alignnone size-full wp-image-460" title="Timing C# function execution time with a stopwatch" src="http://www.dijksterhuis.org/wp-content/uploads/2009/01/stopwatch1.jpg" alt="Timing C# function execution time with a stopwatch" width="499" height="258" /><br />
Occasionally you need to time the performance of a C# function to see if its as fast, or as slow, as you think it is. You could of course run your code through a profiler. But in situations that that would be overkill there exists a quick solution. Originally I looked at the standard DateTime class, but NET 2.0 introduced the  Stopwatch class which is much more suitable. In this post I look at how you can measure your functions performance down to the last fraction of a millisecond. </em></p>
<p><span id="more-458"></span></p>
<p><strong>How not to measure your C# functions execution time</strong></p>
<p>The DateTime.Now function returns the current time on your computer, down to the millisecond. A simple approach to measuring the time of a function would thus be:</p>
<pre class="brush: c#">
DateTime Start = DateTime.Now;

for (int Lp = 0; Lp &lt; 100; Lp++)
Console.WriteLine(&quot;Hello World!&quot;);

TimeSpan Elapsed = DateTime.Now - Start;
Console.WriteLine(&quot;Time Elapsed: {0} ms&quot;,Elapsed.Milliseconds);
</pre>
<p>The problem is that <a rel="nofollow" href="http://msdn.microsoft.com/en-us/library/system.datetime.now.aspx">according to MSDN</a> the resolution of the DateTime.Now function is 10+ milliseconds, and we need to call it twice! So that would introduce a 20+ ms swing in your runtime measurement. As our function calls are often likely to be a lot quicker to return than this 20ms window this isn&#8217;t good enough.</p>
<p>Another approach would be to use the system tickcount as kept by the Environment.TickCount variable, which is also kept in milliseconds. This keeps the number of milliseconds since the computer was started. Unfortunately it is kept in a signed int, so that every 49 days (how often do you reset your computer?) it is actually counting backwards. We can compensate for this by just taking off the most siginificant bit.</p>
<pre class="brush: c#">
int Start = Environment.TickCount &amp;amp;amp;amp; Int32.MaxValue;

for (int Lp = 0; Lp &lt; 100; Lp++)
Console.WriteLine(&quot;Hello World!&quot;);

int Elapsed = (Environment.TickCount &amp;amp;amp;amp; Int32.MaxValue) - Start;
Console.WriteLine(&quot;Time Elapsed: {0} ms&quot;,Elapsed);
</pre>
<p><strong>Using the Stopwatch class</strong></p>
<p>The .NET 2.0 libraries introduced the Stopwatch class in <em>System.Diagnostics</em> which is much better suited for what we intend to do.  The class depends on the hardware available in your computer, if a high resolution timer is available is will use this, otherwise it will fall back to DateTime.Now.</p>
<p>You can check if the Stopwatch is using a high resolution timer by calling <em>Stopwatch.IsHighResolution</em>, and inquiry about the resolution in ticks per second by querying <em>Stopwatch.Frequency</em>.</p>
<pre class="brush: c#">
// If we don&#039;t have a high resolution timer then Stopwatch will fall back
// to DateTime, which is much less reliable
if (Stopwatch.IsHighResolution)
Console.WriteLine(&quot;We have a high resolution timer available&quot;);

long frequency = Stopwatch.Frequency;
Console.WriteLine(&quot; Timer frequency in ticks per second = {0}&quot;,frequency);
</pre>
<p>To start a Stopwatch simply call &#8220;Stopwatch.StartNew()&#8221; as shown below:</p>
<pre class="brush: c#">
// Start the stopwatch
Stopwatch sw = Stopwatch.StartNew();

for (int Lp = 0; Lp &lt; 100; Lp++)
Console.WriteLine(&quot;Hello World!&quot;);

// Stop the stopwatch
sw.Stop();

// Report the results
Console.WriteLine(&quot;Time used (float): {0} ms&quot;,sw.Elapsed.TotalMilliseconds);
Console.WriteLine(&quot;Time used (rounded): {0} ms&quot;,sw.ElapsedMilliseconds);
</pre>
<p>The <em>ElapsedMilliseconds</em> property returns a rounded number to the nearest full millisecond, the <em>Elapsed.TotalMilliseconds</em> property is a float that can return execution times to the partial millisecond.</p>
<p><em>Image credit: <a rel="nofollow" href="http://www.flickr.com/photos/27383466@N02/">Stopwatch by Duy</a></em></p>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/timing-function-performance-stopwatch-class/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
