<?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>Kerry D. Wong &#187; High Resolution Timing</title>
	<atom:link href="http://www.kerrywong.com/tag/high-resolution-timing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kerrywong.com</link>
	<description></description>
	<lastBuildDate>Fri, 03 Sep 2010 00:51:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>A High Resolution Timing Utility in C#</title>
		<link>http://www.kerrywong.com/2005/10/09/a-high-resolution-timing-utility-in-c/</link>
		<comments>http://www.kerrywong.com/2005/10/09/a-high-resolution-timing-utility-in-c/#comments</comments>
		<pubDate>Sun, 09 Oct 2005 11:13:30 +0000</pubDate>
		<dc:creator>kwong</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C Sharp (C#)]]></category>
		<category><![CDATA[High Resolution Timing]]></category>

		<guid isPermaLink="false">http://dimension/2005/10/09/a-high-resolution-timing-utility-in-c/</guid>
		<description><![CDATA[Download PerformanceCounter.zip Assembly level developers have long enjoyed the use of RDTSC (read time stamp counter) instruction to achieve high resolution (1/clock speed (HZ)). And C++ developers have also been using QueryPerformanceCounter and QueryPerformanceFrequency from the Windows API to achieve the same level of resolution. In .Net, we can use these two Windows API functions [...]]]></description>
			<content:encoded><![CDATA[<p>Download <a title="PerformanceCounter.zip" href="/blog/wp-content/uploads/2007/09/performancecounter.zip">PerformanceCounter.zip</a>  Assembly level developers have long enjoyed the use of RDTSC (read time stamp counter) instruction to achieve high resolution (1/clock speed (HZ)).<span id="more-38"></span> And C++ developers have also been using QueryPerformanceCounter and QueryPerformanceFrequency from the Windows API to achieve the same level of resolution.  In .Net, we can use these two Windows API functions as well (see <a href="http://support.microsoft.com/default.aspx/kb/306979/EN-US/">How To Use QueryPerformanceCounter to Time Code in Visual C# .NET</a>), but the interopped calls from the .Net framework seem to have some latency and thus affect the possible resolutions obtained using the API functions. Also, the call is not particularly convenient to make (it has a reference type parameter).</p>
<p>One quick solution is to wrap the API function within a class, but this unfortunately adds more overhead to the call and thus reduces the achievable accuracy even future.  I tried to address the convenience and accuracy issues by providing a customized class. It enhances the accuracy by measuring the average overhead associated with the call. This is shown as follows (in the constructor):<br />
<blockquote style="margin-right: 0px;" dir="ltr"><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;"><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">public</span> PerformanceCounter()    { </span></p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;"><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">   int</span> r <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> QueryPerformanceCounter(<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">ref</span> counterStart);</span>  </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">   if</span> (r == 0) <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">      throw</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">new</span> Exception(<span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);">&quot;High-resolution counter not supported.&quot;</span>);  </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p>Start(); </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p>End();  </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">   for</span> (<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">int</span> i <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> 0 ; i &lt; 100000 ; i++) { </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p>Start(); avgOverHead += End(); </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p>}  </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p>avgOverHead <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> (<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">long</span>) ((<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">double</span>) avgOverHead <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">/</span> 100000.0); </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p>}</p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">public</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">void</span> Start() { </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p>QueryPerformanceCounter(<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">ref</span> counterStart); </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p>}  </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">public</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">long</span> End() { </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p>QueryPerformanceCounter(<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">ref</span> counterEnd);  </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">   return</span> counterEnd <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">-</span> counterStart; </p></blockquote>
<blockquote style="margin-right: 0px;" dir="ltr"><p>}</p></blockquote>
<p> Every time when the class is initialized, the API function is called continuously for 100000 times and the average call overhead is obtained. A function TimeElapsed is provided to calculate the time span between the Start() and the End() call. Note that the first round of the Start() and End() call is ignored because it typically takes longer for the CLR to load the code for the first time.</p>
<p><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;"><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">public</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">double</span> TimeElapsed(<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">string</span> unit) { </span></p>
<p><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;"><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">   double</span> diff <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> counterEnd <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">-</span> counterStart <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">-</span> avgOverHead; </span></p>
<p><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;"><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">   double</span> seconds <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> diff <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">/</span> ((<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">double</span>) Frequency);</span></p>
<p><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">   switch</span> (unit.Trim().ToUpper()) {</p>
<p><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">      case</span> <span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);">&quot;S&quot;</span>: <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">         return</span> seconds;</p>
<p><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">      case</span> <span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);">&quot;MS&quot;</span>: <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">         return</span> seconds <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">*</span> 1000.0;</p>
<p><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">      case</span> <span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);">&quot;US&quot;</span>: <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">         return</span> seconds <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">*</span> 1000000.0;</p>
<p><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">      case</span> <span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);">&quot;NS&quot;</span>: <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">         return</span> seconds <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">*</span> 1000000000.0;</p>
<p><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">      default</span>: <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">         throw</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">            new</span> Exception(<span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);">&quot;Unit &quot;</span> <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">+</span> unit <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">+</span> <span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);">&quot; not supported.&quot;</span>);</p>
<p>}</p>
<p>}</p>
<p>The TimeElapsed function takes the call latency into consideration. It also simplifies the result by converting it to the specified measurement unit (second, millisecond, etc).  The following code sample shows how to use the utility:</p>
<p><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;">PerformanceCounter p <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">new</span> PerformanceCounter(); </span></p>
<p><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;">p.Start(); </span></p>
<p><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;">{code to be timed goes here} </span></p>
<p><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;">p.End(); </span></p>
<p><span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;">Console.WriteLine(p.TimeElapsed(<span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);">&quot;ms&quot;</span>)); </span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kerrywong.com/2005/10/09/a-high-resolution-timing-utility-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
