﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Bluebit Software Support Forum / Technical Support and Help / General  / A High Resolution Performance Timer / Latest Posts</title><generator>InstantForum.NET v4.1.4</generator><description>Bluebit Software Support Forum</description><link>http://www.bluebit.gr/forum/</link><webMaster>support@bluebit.gr</webMaster><lastBuildDate>Thu, 23 May 2013 02:49:42 GMT</lastBuildDate><ttl>20</ttl><item><title>A High Resolution Performance Timer</title><link>http://www.bluebit.gr/forum/Topic58-6-1.aspx</link><description>We publish here some code for a high resolution performance&lt;BR&gt;timer that can be used to measure the performance of your code&lt;BR&gt;to nanosecond accuracy.&lt;BR&gt;&lt;BR&gt;The code is beased on this article [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenethowto09.asp[/url]&lt;BR&gt;but it has been modified so that the timer can be started and&lt;BR&gt;stopped many times and its Duration property will return the&lt;BR&gt;cumulative elapsed time that the timer has been running.&lt;BR&gt;&lt;BR&gt;Normally you will use the PerformanceTimer class as follows:&lt;BR&gt;&lt;UL&gt;&lt;LI&gt;Create a new instance of the &lt;B&gt;PerformanceTimer&lt;/B&gt; class.&lt;/LI&gt;&lt;LI&gt;Call the &lt;B&gt;StartTimer&lt;/B&gt; method.&lt;/LI&gt;&lt;LI&gt;Execute any code you need to measure its duration in time.&lt;/LI&gt;&lt;LI&gt;Call the &lt;B&gt;StopTimer&lt;/B&gt; method.&lt;/LI&gt;&lt;LI&gt;Read the &lt;B&gt;Duration&lt;/B&gt; property to get the cumulative elapsed time the timer has been running.&lt;BR&gt;You may also read this property while the timer is running.&lt;/LI&gt;&lt;LI&gt;Call the &lt;B&gt;Reset&lt;/B&gt; method to set the Duration property to 0.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;[code]using System;&lt;BR&gt;using System.Runtime.InteropServices;&lt;/P&gt;&lt;P&gt;namespace Bluebit.Utility&lt;BR&gt;{&lt;BR&gt;    /// &amp;lt;summary&amp;gt;The %PerformanceTimer% class can be used to measure intervals of time.&amp;lt;/summary&amp;gt;&lt;BR&gt;    /// &amp;lt;remarks&amp;gt;&lt;BR&gt;    /// &amp;lt;para&amp;gt;Normally you will use the &amp;lt;strong&amp;gt;PerformanceTimer&amp;lt;/strong&amp;gt; class is as&lt;BR&gt;    /// follows:&amp;lt;/para&amp;gt;&lt;BR&gt;    /// &amp;lt;list type="bullet"&amp;gt;&lt;BR&gt;    /// &amp;lt;item&amp;gt;Create a new instance of the %PerformanceTimer% class.&amp;lt;/item&amp;gt;&lt;BR&gt;    /// &amp;lt;item&amp;gt;Call the&lt;BR&gt;    /// &amp;lt;a href="Bluebit.Learning~Bluebit.Utility.PerformanceTimer~StartTimer.html"&amp;gt;StartTimer&amp;lt;/a&amp;gt;&lt;BR&gt;    /// method.&amp;lt;/item&amp;gt;&lt;BR&gt;    /// &amp;lt;item&amp;gt;Execute any code you need to measure its duration in time.&amp;lt;/item&amp;gt;&lt;BR&gt;    /// &amp;lt;item&amp;gt;Call the&lt;BR&gt;    /// &amp;lt;a href="Bluebit.Learning~Bluebit.Utility.PerformanceTimer~StopTimer.html"&amp;gt;StopTimer&amp;lt;/a&amp;gt;&lt;BR&gt;    /// method.&amp;lt;/item&amp;gt;&lt;BR&gt;    /// &amp;lt;item&amp;gt;Read the&lt;BR&gt;    /// &amp;lt;a href="Bluebit.Learning~Bluebit.Utility.PerformanceTimer~Duration.html"&amp;gt;Duration&amp;lt;/a&amp;gt;&lt;BR&gt;    /// property to get the cumulative elapsed time the timer has been running. You may&lt;BR&gt;    /// also read this property while the timer is running.&amp;lt;/item&amp;gt;&lt;BR&gt;    /// &amp;lt;item&amp;gt;Call the&lt;BR&gt;    /// &amp;lt;a href="Bluebit.Learning~Bluebit.Utility.PerformanceTimer~Reset.html"&amp;gt;Reset&amp;lt;/a&amp;gt;&lt;BR&gt;    /// method to set the&lt;BR&gt;    /// &amp;lt;a href="Bluebit.Learning~Bluebit.Utility.PerformanceTimer~Duration.html"&amp;gt;Duration&amp;lt;/a&amp;gt;&lt;BR&gt;    /// property to 0.&amp;lt;/item&amp;gt;&lt;BR&gt;    /// &amp;lt;/list&amp;gt;&lt;BR&gt;    /// &amp;lt;/remarks&amp;gt;&lt;BR&gt;    public class PerformanceTimer&lt;BR&gt;    {&lt;BR&gt;        ///////////////////// &lt;BR&gt;        #region PRIVATE FIELDS&lt;BR&gt;        private long _freq; // internal performance-counter frequency, in counts per second&lt;BR&gt;        private long _bias; // the time it takes to execute a StartTimer and a StopTimer command.&lt;BR&gt;        private long _start; // last time the timer was started&lt;BR&gt;        private long _stop; // last time the timer was stopped&lt;BR&gt;        private long _duration; // cumulative elapsed time.&lt;BR&gt;        private bool _running; // current state of the timer. &lt;BR&gt;        #endregion&lt;BR&gt;        /////////////////////&lt;BR&gt;        #region EXTERNAL DECLARATIONS&lt;BR&gt;        [DllImport("kernel32.dll")]&lt;BR&gt;        extern static short QueryPerformanceCounter(ref long x);&lt;BR&gt;        [DllImport("kernel32.dll")]&lt;BR&gt;        extern static short QueryPerformanceFrequency(ref long x);&lt;BR&gt;        #endregion&lt;BR&gt;        /////////////////////&lt;BR&gt;        #region CONSTRUCTOR&lt;BR&gt;        /// &amp;lt;summary&amp;gt;Creates a new instance of the %PerformanceTimer% class&amp;lt;/summary&amp;gt;&lt;BR&gt;        public PerformanceTimer()&lt;BR&gt;        {&lt;BR&gt;            QueryPerformanceFrequency(ref _freq);&lt;BR&gt;            if (_freq == 0)&lt;BR&gt;                throw new ApplicationException("The installed hardware does not support a high-resolution performance counter.");&lt;/P&gt;&lt;P&gt;            // calculate the _bias; the first result maybe inaccurate&lt;BR&gt;            StartTimer();&lt;BR&gt;            StopTimer();&lt;BR&gt;            StartTimer();&lt;BR&gt;            StopTimer();&lt;BR&gt;            _bias = _stop - _start;&lt;BR&gt;            _duration = 0;&lt;BR&gt;        }&lt;BR&gt;        #endregion&lt;BR&gt;        /////////////////////&lt;BR&gt;        #region PUBLIC METHODS&lt;BR&gt;        /// &amp;lt;summary&amp;gt;Starts the timer.&amp;lt;/summary&amp;gt;&lt;BR&gt;        public void StartTimer()&lt;BR&gt;        {&lt;BR&gt;            if (!_running)&lt;BR&gt;            {&lt;BR&gt;                _running = true;&lt;BR&gt;                QueryPerformanceCounter(ref _start);&lt;BR&gt;            }&lt;BR&gt;        }&lt;/P&gt;&lt;P&gt;        /// &amp;lt;summary&amp;gt;&lt;BR&gt;        /// Stops the timer.&lt;BR&gt;        /// &amp;lt;/summary&amp;gt;&lt;BR&gt;        public void StopTimer()&lt;BR&gt;        {&lt;BR&gt;            if (_running)&lt;BR&gt;            {&lt;BR&gt;                QueryPerformanceCounter(ref _stop);&lt;BR&gt;                long d = _stop - _start - _bias;&lt;BR&gt;                d = (d &amp;gt; 0) ? d : 0;&lt;BR&gt;                _duration += d;&lt;BR&gt;                _running = false;&lt;BR&gt;            }&lt;BR&gt;        }&lt;BR&gt;        /// &amp;lt;summary&amp;gt;&lt;BR&gt;        /// Resets the timer.&lt;BR&gt;        /// &amp;lt;/summary&amp;gt;&lt;BR&gt;        /// &amp;lt;remarks&amp;gt;&lt;BR&gt;        /// The %Reset% method sets the %Duration% of the timer to zero. If the timer is&lt;BR&gt;        /// running it is stopped.&lt;BR&gt;        /// &amp;lt;/remarks&amp;gt;&lt;BR&gt;        public void Reset()&lt;BR&gt;        {&lt;BR&gt;            if (_running) StopTimer();&lt;BR&gt;            _duration = 0;&lt;BR&gt;        }&lt;/P&gt;&lt;P&gt;        #endregion&lt;BR&gt;        /////////////////////&lt;BR&gt;        #region PUBLIC PROPERTIES&lt;BR&gt;        /// &amp;lt;summary&amp;gt;Gets the cumulative elapsed time the timer has been running.&amp;lt;/summary&amp;gt;&lt;BR&gt;        /// &amp;lt;value&amp;gt;&lt;BR&gt;        /// A %System.Double% representing the number of seconds the timer has been&lt;BR&gt;        /// running.&lt;BR&gt;        /// &amp;lt;/value&amp;gt;&lt;BR&gt;        public double Duration&lt;BR&gt;        {&lt;BR&gt;            get&lt;BR&gt;            {&lt;BR&gt;                if (_running)&lt;BR&gt;                {&lt;BR&gt;                    QueryPerformanceCounter(ref _stop);&lt;BR&gt;                    long d = _stop - _start - _bias;&lt;BR&gt;                    d = (d &amp;gt; 0) ? d : 0;&lt;BR&gt;                    return (double)(_duration + d) / (double)_freq;&lt;BR&gt;                }&lt;/P&gt;&lt;P&gt;                return (double)_duration / (double)_freq;&lt;BR&gt;            }&lt;BR&gt;        }&lt;BR&gt;        /// &amp;lt;summary&amp;gt;&lt;BR&gt;        /// Gets the current performance-counter frequency, in counts per second.&lt;BR&gt;        /// &amp;lt;/summary&amp;gt;&lt;BR&gt;        public long Frequency&lt;BR&gt;        {&lt;BR&gt;            get { return _freq; }&lt;BR&gt;        }&lt;/P&gt;&lt;P&gt;        /// &amp;lt;summary&amp;gt;&lt;BR&gt;        /// Gets the current state of the timer.&lt;BR&gt;        /// &amp;lt;/summary&amp;gt;&lt;BR&gt;        /// &amp;lt;value&amp;gt;&lt;BR&gt;        /// &amp;lt;strong&amp;gt;true&amp;lt;/strong&amp;gt; if timer is running, &amp;lt;strong&amp;gt;false&amp;lt;/strong&amp;gt; if timer is&lt;BR&gt;        /// stopped.&lt;BR&gt;        /// &amp;lt;/value&amp;gt;&lt;BR&gt;        /// &amp;lt;remarks&amp;gt;&lt;BR&gt;        /// The %StartTimer% method sets this property to &amp;lt;strong&amp;gt;true&amp;lt;/strong&amp;gt; whereas the&lt;BR&gt;        /// %StopTimer% method sets it to &amp;lt;strong&amp;gt;false&amp;lt;/strong&amp;gt;&lt;BR&gt;        /// &amp;lt;/remarks&amp;gt;&lt;BR&gt;        public bool IsRunning&lt;BR&gt;        {&lt;BR&gt;            get { return _running; }&lt;BR&gt;        }&lt;BR&gt;        #endregion&lt;BR&gt;        /////////////////////&lt;BR&gt;    }&lt;BR&gt;}[/code]</description><pubDate>Mon, 02 May 2005 11:06:44 GMT</pubDate><dc:creator>Trifon</dc:creator></item></channel></rss>