C# Класс RTools.Util.HighResClock

This provides access to the Kernel32.dll high resolution clock API. This is motivated by the need to have higher resolution than .Net's DateTime.Now, which is apparently about 10ms. The effective resolution of HighResClock on a P4 1.4GHz is about 10us. System.TimeSpan System.DateTime

WARNING: I don't have any particularly good way of verifying the accuracy of this class. The best I've done is in the TestSelf() which is to compare the measured duration to Sleep(), which is loose. Also, I haven't tested this on any other PCs. I understand that the high performance counter may not be available on all systems.

The "ticks" in this class DO correspond to ticks in DateTime and TimeSpan, except in the Frequency property which is explicitly the high performance counter frequency.

It's relatively expensive to read this clock, compared to DateTime.Now. Although it's only about 10us on a P4 1.4GHz, it's faster to use DateTime.Now.

This does a calibration to determine function call overhead time, which is a static field. This has a static constructor which calls Calibrate(). Calibrate() temporarily boosts the calling process and thread priority to avoid being affected by other processes/threads.

The duration calculations are adjusted by the function call overhead time calculated during Calibrate(), but Now, NowTicks, etc are not.

You can use this just like DateTime for timing durations: DateTime startTime = HighResClock.Now; // do something you want to time here TimeSpan duration = HighResClock.Now - startTime; Console.WriteLine("Duration: {0}ms", duration.TotalMilliseconds);

To compensate for the overhead in the high resolution clock reads, you can use the following: DateTime startTime = HighResClock.Now; // do something you want to time here TimeSpan duration = HighResClock.CalcTimeSpan(startTime); Console.WriteLine("Duration: {0}ms", duration.TotalMilliseconds);

Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
CalcTimeSpan ( System.DateTime startTime ) : System.TimeSpan

Calculate the duration (to NowTicks) as a TimeSpan. System.TimeSpan

This subtracts OverheadTicks from the measured ticks to compensate for overhead.

CalcTimeSpan ( System.DateTime startTime, System.DateTime stopTime ) : System.TimeSpan

Calculate the duration (to NowTicks) as a TimeSpan. System.TimeSpan

This subtracts OverheadTicks from the measured ticks to compensate for overhead.

CalcTimeSpan ( long startTicks ) : System.TimeSpan

Calculate the duration (to NowTicks) as a TimeSpan. System.TimeSpan

This subtracts OverheadTicks from the measured ticks to compensate for overhead.

CalcTimeSpan ( long startTicks, long stopTicks ) : System.TimeSpan

Calculate the duration as a TimeSpan. System.TimeSpan

This subtracts OverheadTicks from the measured ticks to compensate for overhead.

Calibrate ( ) : void

Measure overhead for interop calls and clock resolution. The resultant values are stored in static fields. The resultant clock frequency and function call overhead measurement are used in ticks to ms conversion and duration calculations, respectively.

This is called from the static constructor, so you typically won't benefit from calling it, unless during construction there were other very high priority things going on that affected the timing of the statements in this method.

This temporarily boosts this process and thread priority to avoid being affected by other processes/threads, so you definitely don't want to call this a lot.

TestSelf ( ) : bool

A simple self test.

TicksToMs ( long ticks ) : float

Convert ticks to milliseconds. (A tick is 100 nanoseconds.)

No adjustment is made to this due to OverheadTicks.

Приватные методы

Метод Описание
HighResClock ( ) : System

Not supposed to instantiate this class.

K32QueryPerformanceCounter ( long &t ) : uint
K32QueryPerformanceFrequency ( long &t ) : uint

Описание методов

CalcTimeSpan() публичный статический Метод

Calculate the duration (to NowTicks) as a TimeSpan. System.TimeSpan
This subtracts OverheadTicks from the measured ticks to compensate for overhead.
public static CalcTimeSpan ( System.DateTime startTime ) : System.TimeSpan
startTime System.DateTime The starting DateTime.
Результат System.TimeSpan

CalcTimeSpan() публичный статический Метод

Calculate the duration (to NowTicks) as a TimeSpan. System.TimeSpan
This subtracts OverheadTicks from the measured ticks to compensate for overhead.
public static CalcTimeSpan ( System.DateTime startTime, System.DateTime stopTime ) : System.TimeSpan
startTime System.DateTime The starting DateTime.
stopTime System.DateTime The stopping DateTime.
Результат System.TimeSpan

CalcTimeSpan() публичный статический Метод

Calculate the duration (to NowTicks) as a TimeSpan. System.TimeSpan
This subtracts OverheadTicks from the measured ticks to compensate for overhead.
public static CalcTimeSpan ( long startTicks ) : System.TimeSpan
startTicks long The starting tick count.
Результат System.TimeSpan

CalcTimeSpan() публичный статический Метод

Calculate the duration as a TimeSpan. System.TimeSpan
This subtracts OverheadTicks from the measured ticks to compensate for overhead.
public static CalcTimeSpan ( long startTicks, long stopTicks ) : System.TimeSpan
startTicks long The starting tick count.
stopTicks long The stopTicks tick count.
Результат System.TimeSpan

Calibrate() публичный статический Метод

Measure overhead for interop calls and clock resolution. The resultant values are stored in static fields. The resultant clock frequency and function call overhead measurement are used in ticks to ms conversion and duration calculations, respectively.

This is called from the static constructor, so you typically won't benefit from calling it, unless during construction there were other very high priority things going on that affected the timing of the statements in this method.

This temporarily boosts this process and thread priority to avoid being affected by other processes/threads, so you definitely don't want to call this a lot.

public static Calibrate ( ) : void
Результат void

TestSelf() публичный статический Метод

A simple self test.
public static TestSelf ( ) : bool
Результат bool

TicksToMs() публичный статический Метод

Convert ticks to milliseconds. (A tick is 100 nanoseconds.)
No adjustment is made to this due to OverheadTicks.
public static TicksToMs ( long ticks ) : float
ticks long The tick count.
Результат float