12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using System.Diagnostics;
- namespace AIDiagnosisDemo.GC
- {
- internal class CpuClock
- {
- #region Fields and Events
- // Fields
- private static double _cpuFrequency;
- private double _startTime;
- #endregion Fields and Events
- #region Methods
- // Constructors
- public CpuClock()
- {
- _startTime = GetCurrentTimestamp();
- }
- static CpuClock()
- {
- long counter;
- counter = Stopwatch.Frequency;
- _cpuFrequency = counter;
- }
- // Methods
- private static long QueryPerformanceCounter()
- {
- long counter;
- counter = Stopwatch.GetTimestamp();
- return counter;
- }
- private static long QueryPerformanceFrequency()
- {
- long cpuFrequency;
- cpuFrequency = Stopwatch.Frequency;
- return cpuFrequency;
- }
- private static double GetCurrentTimestamp()
- {
- long newCounter = QueryPerformanceCounter();
- return newCounter / (double)_cpuFrequency;
- }
- /// <summary>
- /// Returns the spent time(as second) after the <c>CpuClock</c> is created
- /// </summary>
- /// <returns></returns>
- public double GetTime()
- {
- return TotalSeconds;
- }
- /// <summary>
- /// Returns the spent time(as second) after system start
- /// </summary>
- /// <returns></returns>
- public static double Current
- {
- get { return GetCurrentTimestamp(); }
- }
- public double StartTime
- {
- get { return _startTime; }
- }
- public double TotalSeconds
- {
- get { return (Current - _startTime); }
- }
- public double TotalMilliSeconds
- {
- get { return (Current - _startTime) * 1000; }
- }
- public static CpuClock Now
- {
- get
- {
- return new CpuClock();
- }
- }
- #endregion Methods
- }
- }
|