#region Using using System; using System.Runtime.InteropServices; #endregion namespace Wankuma.Interop { /// /// QueryPerformanceCounter を使ったパフォーマンスカウンタです。 /// public class InteropQueryPerformanceCounter { #region P/Invoke [DllImport("kernel32.dll")] extern static short QueryPerformanceCounter(ref long x); [DllImport("kernel32.dll")] extern static short QueryPerformanceFrequency(ref long x); #endregion #region メンバ private long _StratTime = 0; private long _EndTime = 0; private long _Frequency = 0; #endregion #region public long Frequency /// /// 分解能を取得します。 /// /// このシステムではQueryPerformanceCounterが対応していません。 public long Frequency { get { if (this._Frequency != 0 ) { return this._Frequency; } else { if ( QueryPerformanceFrequency(ref this._Frequency) == 0 ) { throw new NotSupportedException("このシステムでは対応していません。"); } else { return this._Frequency; } } } } #endregion #region public void Start() /// /// 計測を開始します /// /// このシステムではQueryPerformanceCounterが対応していません。 public void Start() { if ( QueryPerformanceCounter(ref this._StratTime) == 0 ) { throw new NotSupportedException("このシステムでは対応していません。"); } } #endregion #region public void End() /// /// 計測を終了します /// /// このシステムではQueryPerformanceCounterが対応していません。 public void End() { if ( QueryPerformanceCounter(ref this._EndTime) == 0 ) { throw new NotSupportedException("このシステムでは対応していません。"); } } #endregion #region public double Result /// /// 計測結果を返します /// /// このシステムではQueryPerformanceCounterが対応していません。 public double Result { get { return (this._EndTime - this._StratTime) * 1.0 / this.Frequency; } } #endregion } }