Rolando Ramirez Weblog @ DotNetJunkies

<January 2009>
SuMoTuWeThFrSa
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567


Navigation

Subscriptions

Post Categories



DotNetRolando.Stopwatch v1.0.0 - My own (simple) implementation of .net v2.0's System.Diagnostics.Stopwatch

//
// DotNetRolando.Stopwatch
//   v1.0.0
//
// My own (simple) implementation of .net v2.0's System.Diagnostics.Stopwatch
//   http://msdn2.microsoft.com/library/System.Diagnostics.Stopwatch.aspx
//   
//   Critics and suggestions are welcome.
//
// Changes Log:
//   Version     Date
//   =======  ==========
//    1.0.0   2004-09-27  First Version. A simple one.
//                         * Only HighFrequency timer is implemented. A Win32Exception is thrown if the
//                             OS doesn't support HighFrequency.
//                         * Method "GetTimeStamp" is not implemented. Still trying to figure out what
//                             this method returns.
//                             http://msdn2.microsoft.com/library/System.Diagnostics.Stopwatch.GetTimeStamp.aspx
//

using System.Runtime.InteropServices;

namespace DotNetRolando {

    public class Stopwatch {

#region APIs Declaration

    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);

    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceFrequency(out long lpFrequency);

#endregion //APIs Declaration

        private long _frequency;
        private bool _isRunning;
        
        private long startTime, stopTime;
        private long totalTime;

        public long Frequency {
            get {
                return _frequency;
            }    
        }

        public bool IsRunning {
            get {
                return _isRunning;
            }    
        }
        
        public bool IsHighResolution {
            get {
                // Only HighFrequency timer is implemented.
                return true;
            }    
        }

        public  System.TimeSpan Elapsed {
            get {
                if (_isRunning) {
                    QueryPerformanceCounter(out stopTime);
                    totalTime = totalTime + (stopTime - startTime);
                }
                return new System.TimeSpan(totalTime);
            }
        }

        public long ElapsedTicks {
            get {
                if (_isRunning) {
                    QueryPerformanceCounter(out stopTime);
                    totalTime = totalTime + (stopTime - startTime);
                }
                return totalTime;
            }
        }

        public double ElapsedMilliseconds {
            get {
                if (_isRunning) {
                    QueryPerformanceCounter(out stopTime);
                    totalTime = totalTime + (stopTime - startTime);
                }
                return ((double)(totalTime)) / ((double)(_frequency));
            }
        }

        public Stopwatch() {
            _isRunning = false;
            startTime = 0;
            stopTime  = 0;
            totalTime = 0;

            if (!(QueryPerformanceFrequency(out _frequency))) {
                // Only HighFrequency timer is implemented.
                throw new System.ComponentModel.Win32Exception();
            }
        }

        public void Start() {
            if (!_isRunning) {
                _isRunning = true;
                System.Threading.Thread.Sleep(0);
                QueryPerformanceCounter(out startTime);
            }
        }

        public void StartNew() {
            startTime = 0;
            stopTime  = 0;
            totalTime = 0;

            _isRunning = true;
            System.Threading.Thread.Sleep(0);
            QueryPerformanceCounter(out startTime);
        }

        public void Stop() {
            if (_isRunning) {
                QueryPerformanceCounter(out stopTime);
                totalTime = totalTime + (stopTime - startTime);
                _isRunning = false;
            }
        }

        public void Reset() {
            _isRunning = false;
            startTime = 0;
            stopTime  = 0;
            totalTime = 0;
        }
    }
}

posted on Tuesday, September 28, 2004 8:36 AM by rramirezg





Powered by Dot Net Junkies, by Telligent Systems